Updated March 24, 2023
Introduction to Reverse String in Java
A string is a character sequence that is an object in Java. There are several operations that can be performed on the String object in Java. One of the commonly used operations is String Reversal. A String that is reversed is said to be a Reverse String. For example, the ‘HAPPY’ string can be reversed as ’YPPAH’. Similarly, the ‘LUCKY’ string can be written as ‘YKCUL’ when it is reversed.
Logic Behind Reverse String in Java
There are several ways in which a string can be reversed. For each of the methods, separate functions and loops will be used. Even though the methods are different, the underlying process behind the reversal of string almost similar, and it is mentioned below:
- Input a string either in code or using user input.
- Take the characters of the string starting from the last position and store them in any variable.
- Display the final output.
How to Reverse String in Java using Various Methods?
There are several methods in order to perform the reversal of string.
- Using StringBuffer or StringBuilder
- Using charAt() method
- Using ArrayList object
- Using Reverse Iteration
- Using Recursion
Let us look into each of them in detail.
1. Program to Reverse a String using StringBuffer or StringBuilder
Code:
//Java program to Reverse a String using StringBuffer
//class
public class ReverseStringExample{
//main method
public static void main(String[] args) {
//string
String str1 = "Annamu";
//reversed string will be stored in rev variable
String rev = new StringBuffer(str1).reverse().toString();
//print string before reverse
System.out.println("\nString before reversal : "+str1);
//print string after reverse
System.out.println("String after reversal : "+rev);
}
}
Output:
Here, the string that is given as input will be buffered using the StringBuffer, and it will be reversed using the reverse() method. Once the buffer is reversed, it will be converted to a string with the help of the toString() method.
Similarly, StringBuilder can also be used instead of StringBuffer, which is explained in the below program.
Java Program to Reverse a String using StringBuilder.
Code:
//class
public class ReverseStringExample{
//main method
public static void main(String[] args) {
//string
String str1 = "Annamu";
//reversed string will be stored in rev variable
String rev = new StringBuilder(str1).reverse().toString();
//print string before reverse
System.out.println("\nString before reversal : "+str1);
//print string after reverse
System.out.println("String after reversal : "+rev);
}
}
Output:
Here also, it can be seen that the string is reversed even though StringBuilder is used.
2. Program to reverse a string using the charAt() method
Code:
//class
public class ReverseStringExample{
//main method
public static void main(String[] args) {
//string
String str1 = "Java Programming";
//reversed string will be stored in reverseS variable
String reverseS = "";
//length of string will be stored in len
int len=str1.length();
for(int i=len-1;i>=0;i--)
reverseS = reverseS + str1.charAt(i);
//print string before reverse
System.out.println("\nString before reversal : "+str1);
//print string after reverse
System.out.println("String after reversal : "+ reverseS);
}
}
Output:
In this program, the input string characters will be stored in the reverseS variable starting from the last position with the help of for loop. Once all the characters are stored, the reversed string will be displayed.
3. Program to reverse a string using ArrayList object
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
//class
public class ReverseStringExample{
//main method
public static void main(String[] args) {
//string
String str1 = "Programming Language";
//conversion of input string to character array
char ch[]=str1.toCharArray();
//add character array to object of the ArrayList
List<Character> obj = new ArrayList<>();
for (char c: ch)
obj.add(c);
//pass the object of arraylist to collections
Collections.reverse(obj);
//create objecct of listiterator to iterate on list
ListIterator objli = obj.listIterator();
//print string before reversal
System.out.println("\nString before reversal : "+ str1);
System.out.println("\nString after reversal : ");
while (objli.hasNext())
System.out.print(objli.next());
}
}
Output:
In this program, characters of the input string will be stored in the ArrayList object. With the help of the reverse () method in the collections, it gets reversed. Once it is reversed, the iterator iterates over the list and prints the reversed string.
4. Program to reverse a string using Reversive Iteration
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
//class
public class ReverseStringExample{
//main method
public static void main(String[] args) {
//string
String str1 = "Hello World";
//conversion of input string to character array
char ch[]=str1.toCharArray();
//reversed string will be stored in reverseS variable
String reverseS = "";
//For loop for reversal of a string
for(int i=ch.length-1;i>=0;i--)
{
reverseS+=ch[i];
}
//print string before reversal
System.out.println("\nString before reversal : "+ str1);
System.out.println("\nString after reversal : " +reverseS);
}
}
Output:
In this program also, characters of the input string will be stored in the reverseS variable starting from the last position with the help of for loop. Once all the characters are stored, the reversed string will be displayed.
5. Program to Reverse a String using Recursion
Code:
//class
public class ReverseStringExample{
//function reversal
String rev(String str1) {
//if length of the string is zero, return null
if(str1.length() == 0)
{
return " ";
}
//else return the character obtained on following method
return str1.charAt(str1.length()-1) + rev(str1.substring(0,str1.length()-1));
}
public static void main(String[ ] args)
{
// create object of class
ReverseStringExample r=new ReverseStringExample();
//input string
String str1 = "Java Program";
//print string before reversal
System.out.println("\nString before reversal : "+ str1);
//print string after reversal
System.out.println("\nString after reversal : " + r.rev(str1));
}
Output:
In this program, a recursive function is created, which checks the length of the string and reverse it based on that.
Conclusion
String reversal is one of the most common operations of String that is applied in different situations. In this document, several aspects such as algorithm, methods, implementation, etc. of Reversal of String are covered.
Recommended Articles
This has been a guide to Reverse String in Java. Here we discuss the introduction, logic, how to reverse a string in Java using various methods. You may also have a look at the following articles to learn more –