Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gauravkukade/c1cee5b55fa5ee1554b68c31797d2e33 to your computer and use it in GitHub Desktop.
Save gauravkukade/c1cee5b55fa5ee1554b68c31797d2e33 to your computer and use it in GitHub Desktop.
A Java program to reverse a string. We are using 'reverse()' method of the Collections. This method reverse the order of elements of the list specified. The program is embedded at https://coderolls.com/reverse-a-string-in-java
/**
* A Java program to reverse a string.
*
* We are using 'reverse()' method of the Collections. This method reverse the
* order of elements of the list specified.
*
* @author Gaurav Kukade at coderolls.com
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ReverseStringUsingReeverseMethodOfCollections {
public static void main(String[] args) {
String blogName = "coderolls.com";
char [] stringCharArray = blogName.toCharArray();
List<Character> arrayList = new ArrayList<>();
String reversedString = "";
for(char ch : stringCharArray) {
arrayList.add(ch);
}
Collections.reverse(arrayList);
for( Character ch: arrayList) {
reversedString = reversedString + ch;
}
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