{"id":2037,"date":"2017-05-18T05:53:58","date_gmt":"2017-05-18T05:53:58","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2037"},"modified":"2024-11-16T14:56:19","modified_gmt":"2024-11-16T14:56:19","slug":"java-program-for-reverse-of-a-string","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-program-for-reverse-of-a-string\/","title":{"rendered":"Java Program for Reverse of a String"},"content":{"rendered":"<h1>Overview<\/h1>\r\n<p>In this post, we will learn to write a Java program to reverse of a string using <strong>for loop.\u00a0<\/strong><strong>charAt()<\/strong> method is an inbuilt String method that returns the character specified in the index.\u00a0<\/p>\r\n<p>Tools used:<\/p>\r\n<ul>\r\n<li>JDK<\/li>\r\n<li>Eclipse IDE<\/li>\r\n<\/ul>\r\n<p>&nbsp;<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2104\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-a-String.png\" alt=\"\" width=\"1662\" height=\"815\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-a-String.png 1662w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-a-String-300x147.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-a-String-1024x502.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-a-String-768x377.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-a-String-1536x753.png 1536w\" sizes=\"auto, (max-width: 1662px) 100vw, 1662px\" \/><\/p>\r\n<h2>Description<\/h2>\r\n<p>In this program, we have taken an input string from the user. In a loop, we have printed each character is the reverse fashion using a for loop.\u00a0\u00a0<strong>charAt()<\/strong> method is an inbuilt String method that returns the character specified in the index.<\/p>\r\n<p>For example, if the string variable is <strong>str<\/strong> with contents:<\/p>\r\n<p><strong>String str = &#8220;TestingDocs&#8221;;<\/strong><\/p>\r\n<p>str.charAt(7) would return the character <strong>&#8216;D&#8217;<\/strong>. The input parameter for the charAt() is Integer index. This value should be within some bounds for the method to work. The method might throw an exception <strong>StringIndexOutOfBoundsException<\/strong> if the value is negative or not less than the length of the string.\u00a0<\/p>\r\n<p>For example, in the above example str.charAt(17) would result in\u00a0<\/p>\r\n<p>java.lang.StringIndexOutOfBoundsException String index out of range Exception.\u00a0<\/p>\r\n<h2>Eclipse IDE Setup<\/h2>\r\n<p>Some steps and instructions to create a Java application on Eclipse IDE:<\/p>\r\n<ul>\r\n<li>Create Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Package in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/\">https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Class in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/\">https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/<\/a><\/p>\r\n<ul>\r\n<li>Run Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/<\/a><\/p>\r\n<h3>Program Code<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner;\r\n\r\n\/**************************************************\r\n * ReverseOfString.java\r\n * @program : Java program to reverse a String\r\n * @web : www.TestingDocs.com\r\n * @author :\r\n * @version : 1.0\r\n **************************************************\/ \r\n\r\nclass ReverseOfString { \r\n  public static void main(String[ ] arg) { \r\n    String str; \r\n    char ch; \r\n    Scanner sc=new Scanner(System.in); \r\n    System.out.print(\"Enter a string : \"); \r\n    str=sc.nextLine(); \r\n    System.out.println(\"Reverse of a String '\"+str+\"' is :\"); \r\n    for(int j=str.length();j&gt;0;--j) {\r\n      System.out.print(str.charAt(j-1)); \r\n    } \r\n  } \r\n}<\/pre>\r\n<p>&nbsp;<\/p>\r\n<h2>Screenshot<\/h2>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2051 size-full\" title=\"Reverse of a string\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-of-String.png\" alt=\"Reverse of a string\" width=\"1733\" height=\"1002\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-of-String.png 1733w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-of-String-300x173.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-of-String-1024x592.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-of-String-768x444.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Reverse-of-String-1536x888.png 1536w\" sizes=\"auto, (max-width: 1733px) 100vw, 1733px\" \/><\/p>\r\n<h2>Output<\/h2>\r\n<p>Enter a string: how are you<br \/>Reverse of a String &#8216;how are you&#8217; is :<br \/>uoy era woh<\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&#8212;<\/p>\r\n<p>Java Tutorial on this website: <strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\r\n<p>For more information on Java, visit the official website :<\/p>\r\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\r\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2037\" data-postid=\"2037\" class=\"themify_builder_content themify_builder_content-2037 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this post, we will learn to write a Java program to reverse of a string using for loop.\u00a0charAt() method is an inbuilt String method that returns the character specified in the index.\u00a0 Tools used: JDK Eclipse IDE &nbsp; Description In this program, we have taken an input string from the user. In a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2037","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2037"}],"version-history":[{"count":11,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2037\/revisions"}],"predecessor-version":[{"id":21763,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2037\/revisions\/21763"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}