How to Reverse a String in Java
How to Reverse a String in Java
There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a given string. Let’s see different ways to the string reversal.
By using StringBuilder
Consider the following program.
FileName: StringReverseExample.java
public class StringReverseExample { // main method public static void main(String argvs[]) { // input string StringBuilder str = new StringBuilder("I am not a terrorist."); System.out.println("The given string is :"); System.out.println(str + "\n"); // invoking the reverse() method str.reverse(); System.out.println("The string after reversal is :"); System.out.println(str); } }
Output:
The given string is : I am not a terrorist. The string after reversal is : .tsirorret a ton ma I
By using StringBuffer
Consider the following program.
FileName: StringReverseExample1.java
public class StringReverseExample1 { // main method public static void main(String argvs[]) { // input string StringBuffer str = new StringBuffer("I am not a terrorist."); System.out.println("The given string is :"); System.out.println(str + "\n"); // invoking the reverse() method str.reverse(); System.out.println("The string after reversal is :"); System.out.println(str); } }
Output:
The given string is : I am not a terrorist. The string after reversal is : .tsirorret a ton ma I
By using Char Array
Consider the following program.
FileName: StringReverseExample2.java
// A Java program that shows how to use char array to do the reversal of the given string class StringReverse { // method to do the string reversal public String reverseString(String s) { // converting to char array char c[] = s.toCharArray(); String ans=""; // calculating length of the string s int length = c.length; // iterating over the characters of the given string for(int j = length - 1; j >= 0; j--) { ans += c[j]; } return ans; } } public class StringReverseExample2 { // main method public static void main(String argvs[]) { // input string String str = "I am not a terrorist."; // creating an object of the StringReverse clas StringReverse sr = new StringReverse(); System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str); System.out.println("The reverse string is: " + str + "\n"); str = "I am a good person."; System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str); System.out.println("The reverse string is: " + str + "\n"); } }
Output:
The given string is: I am not a terrorist. The reverse string is: .tsirorret a ton ma I The given string is: I am a good person. The reverse string is: .nosrep doog a ma I
Explanation: In the above program, the string is converted into the character array. Then, the character array is traversed from the end to the beginning, i.e., in reverse order to generate the reverse of the given string.
By using the charAt() method
The charAt() has the following syntax.
Syntax:
public char charAt(int index)
FileName: StringReverseExample3.java
// A Java program that shows how to use the charAt() to do the reversal of the given // string class StringReverse { // method to do the string reversal public String reverseString(String s) { String ans=""; // calculating length of the string s int length = s.length(); // iterating over the characters of the given string for(int j = length - 1; j >= 0; j--) { ans += s.charAt(j); } return ans; } } public class StringReverseExample3 { // main method public static void main(String argvs[]) { // input string String str = "I am not a terrorist."; // creating an object of the StringReverse clas StringReverse sr = new StringReverse(); System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str); System.out.println("The reverse string is: " + str + "\n"); str = "I am a good person."; System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str); System.out.println("The reverse string is: " + str + "\n"); } }
Output:
The given string is: I am not a terrorist. The reverse string is: .tsirorret a ton ma I The given string is: I am a good person. The reverse string is: .nosrep doog a ma I
Explanation: The charAt(j) method takes returns the character present at the index j. Since the for-loop is iterating from the last index of the string to the first index. Therefore, at first, the character present at the last index gets copied. Then the character present at the penultimate index and so on. After the end of the for-loop, the string ans stores the string, which is the reverse of the string that is passed as the parameter of the method reverseString().
By using stack
Stack can also be used for the reversal of a given string. The following program illustrates the same.
FileName: StringReverseExample4.java
// A Java program that shows how to use the stack to do the reversal of the given string import java.util.Stack; class StringReverse { // method to do the string reversal public String reverseString(String s) { String ans=""; // calculating length of the string s int length = s.length(); // creating a stack Stack<Character> stk = new Stack(); // iterating over the characters of the given string for(int j = 0; j < length; j++) { stk.push(s.charAt(j)); } // iterate till the stack becomes empty while(!stk.empty()) { // taking the top element from // the stack and concatenating it to // the string ans ans += stk.peek(); // removing the peek element from the stack stk.pop(); } // return the result return ans; } } public class StringReverseExample4 { // main method public static void main(String argvs[]) { // input string String str = "I am not a terrorist."; // creating an object of the StringReverse clas StringReverse sr = new StringReverse(); System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str); System.out.println("The reverse string is: " + str + "\n"); str = "I am a good person."; System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str); System.out.println("The reverse string is: " + str + "\n"); } }
Output:
The given string is: I am not a terrorist. The reverse string is: .tsirorret a ton ma I The given string is: I am a good person. The reverse string is: .nosrep doog a ma I
Explanation: The LIFO (Last In First Out) property of the stack comes in handy to do the reversal of the given string. The for-loop of the reverseString() method puts the characters, one by one, starting from the first index to the last index. Thus, when a character is popped from the stack, the last character comes out first, then the second last character, and so on. Thus, after the end of the while-loop, the string contained in the ans, is the reverse of the string that is passed as the parameter of the method reverseString().
By using recursion
The recursive approach can also be used to do the reverse of the given string. The following program illustrates the same.
FileName: StringReverseExample5.java
// A Java program that shows how to use recursion to do the reversal of the given string class StringReverse { // method to do the string reversal using recursion public String reverseString(String s, int i, int size) { // handling the base case if(i >= size) { return ""; } String ans=""; // recursively calling the method reverseString() ans += reverseString(s, i + 1, size) + s.charAt(i); // return the result return ans; } } public class StringReverseExample5 { // main method public static void main(String argvs[]) { // input string String str = "I am not a terrorist."; // calculating the size of the string int size = str.length(); // creating an object of the StringReverse clas StringReverse sr = new StringReverse(); System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str, 0, size); System.out.println("The reverse string is: " + str + "\n"); str = "I am a good person."; // calculating the size of the string size = str.length(); System.out.println("The given string is: " + str); // invoking the reverseString() method str = sr.reverseString(str, 0, size); System.out.println("The reverse string is: " + str + "\n"); } }
Output:
The given string is: I am not a terrorist. The reverse string is: .tsirorret a ton ma I The given string is: I am a good person. The reverse string is: .nosrep doog a ma I
Explanation: Using recursion, first, we are moving to the end of the given string. Then, the concatenation work starts using the + operator with the help of the method charAt(). Thus, at first, the last character, then the second last character gets concatenated, and eventually, we get a reverse of the given string.