This article discusses different ways to reverse a string in Java with examples.
Examples:
Following are some interesting facts about String and StringBuilder classes :
- Objects of String are immutable.
- String class in Java does not have reverse[] method, however, the StringBuilder class has built-in reverse[] method.
- StringBuilder class do not have toCharArray[] method, while String class does have toCharArray[] method.
Implementation:
Java
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main [String[] args] {
String str= "Geeks", nstr="";
char ch;
System.out.print["Original word: "];
System.out.println["Geeks"];
for [int i=0; i= 0; i--]
System.out.print[try1[i]];
}
}
- Convert the input string into character array by using the toCharArray[]: Convert the input string into character array by using the toCharArray[] – built in method of the String Class. Then, scan the character array from both sides i.e from the start index [left] as well as from last index[right] simultaneously.
Implementation:
Java
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString {
public static void main[String[] args]
{
String input = "Geeks For Geeks";
char[] temparray = input.toCharArray[];
int left, right = 0;
right = temparray.length - 1;
for [left = 0; left < right; left++, right--] {
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
for [char c : temparray]
System.out.print[c];
System.out.println[];
}
}
- Using ArrayList object: Convert the input string into the character array by using toCharArray[] built in method. Then, add the characters of the array into the ArrayList object. Java also has built in reverse[] method for the Collections class. Since Collections class reverse[] method takes a list object, to reverse the list, we will pass the ArrayList object which is a type of list of characters.
Implementation:
Java
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString {
public static void main[String[] args]
{
String input = "Geeks For Geeks";
char[] hello = input.toCharArray[];
List trial1 = new ArrayList[];
for [char c : hello]
trial1.add[c];
Collections.reverse[trial1];
ListIterator li = trial1.listIterator[];
while [li.hasNext[]]
System.out.print[li.next[]];
}
}
Using StringBuffer:
String class does not have reverse[] method, we need to convert the input string to StringBuffer, which is achieved by using the reverse method of StringBuffer.
Implementation:
Java
import java.lang.*;
import java.io.*;
import java.util.*;
public class Test {
public static void main[String[] args]
{
String str = "Geeks";
StringBuffer sbr = new StringBuffer[str];
sbr.reverse[];
System.out.println[sbr];
}
}
- Reversing String by taking input from user-
Java
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main [String[] args] {
Scanner scanner = new Scanner[System.in];
String Str = scanner.nextLine[];
char[] arr = Str.toCharArray[];
String rev = "";
for[int i = Str.length[] - 1; i >= 0; i--]
{
rev = rev + Str.charAt[i];
}
System.out.println[rev];
}
}
In the above code, we are essentially reading a String from the user before starting an iteration loop to create a new, inverted String. The “charAt” function of the String class is used to retrieve each character of the original String individually from the end, and the “+” operator is used to concatenate them into a new String.
Related Article: Different methods to reverse a string in C/C++
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks