Created
August 12, 2019 12:33
-
-
Save gauravkukade/a757716586e488da6e6dc1a995e2bd10 to your computer and use it in GitHub Desktop.
A Java program to reverse a string. We are using 'toCharArray' method of the String class to get char array of the specified string and append it with the temp string in reverse order. The program is embedded at https://coderolls.com/reverse-a-string-in-java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Java program to reverse a string. | |
* | |
* We are using 'toCharArray' method of the String class to get char array of the specified string | |
* and append it with the temp string in reverse order. | |
* | |
* @author Gaurav Kukade at coderolls.com | |
*/ | |
public class ReverseStringUsingToCharArray { | |
public static void main(String[] args) { | |
String blogName = "coderolls.com"; | |
char [] stringCharArray = blogName.toCharArray(); | |
String reversedString = ""; | |
for(int i = stringCharArray.length-1; i>=0; i--) { | |
reversedString = reversedString + stringCharArray[i]; | |
} | |
System.out.print("The reversed string of the '"+blogName+"' is: " ); | |
System.out.println(reversedString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment