Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created August 12, 2019 12:33
Show Gist options
  • Save gauravkukade/a757716586e488da6e6dc1a995e2bd10 to your computer and use it in GitHub Desktop.
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
/**
* 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