How To Reverse a String in Java

In this post, we will learn How to reverse a string in java using different methods.

String is a sequence of characters which behave like an object in java.

There are different ways to reverse a string like using toCharArrray() method, using reverse method of StringBuffer.

 

Reverse in Java

Example: Reverse of WORLD string is DLROW

String is a sequence of characters which behave like an object in java.

There are different ways to reverse a string like using toCharArrray() method, using reverse method of StringBuffer.

 

Reverse in Java

Example: Reverse of WORLD string is DLROW

String is a sequence of characters which behave like an object in java.

There are different ways to reverse a string like using toCharArrray() method, using reverse method of StringBuffer.

 

Reverse in Java

Example: Reverse of WORLD string is DLROW

String is a sequence of characters which behave like an object in java.

There are different ways to reverse a string like using toCharArrray() method, using reverse method of StringBuffer etc.

Reverse in Java

Example: Reverse of WORLD string is DLROW

Using toCharArray() method OR Reverse a string in java using for loop

This method converts the string into array of character. Then using length method we find the length of character into char array. Then using for loop we iterate each character into reverse order.

package demopkg;

public class ReverseString {
    
    public static void main(String[] args) {
        
                
        String str = "Java is a Programming Language";
        char[] c= str.toCharArray();
        
        for(int i=c.length-1; i>=0; i--) {
            
        System.out.print(c[i]);
        }
        
    }

}

Output:

 


Using StringBuffer reverse() method in java

There is a in-built method called reverse() which reverse a string.

In below code, we have created object of StringBuffer class and then using that object we used reverse method.

Code:

package demopkg;

public class ReverseString {
    
    public static void main(String[] args) {
        
        StringBuffer  b= new StringBuffer("hello Java");
      System.out.println(b.reverse());	
        
                
    }

}

Output:

Reverse string using StringBuffer

 

 Note: Also we can use Stringbuilder class to reverse the string using reverse method. 

By Using chatAt() method

Using this method we can print the character of any index of the string.

Using length() method, we will find out the length of string then print the char of the string.

Code:

package demopkg;

public class ReverseString {
    
    public static void main(String[] args) {
        
        String str = "Java is object oriented language";
        
        int lenStr= str.length();
        
        while(lenStr >0) {
        	
        	System.out.print(str.charAt(lenStr-1));
        	lenStr--;
        	
        }
                
    }

}

Output:

 

Reverse string using CharAt method


Reverse a string in java using Scanner

Using scanner class we can take the input from user and then using for loop and charAt() method we can reverse the string.

Code:

package demopkg;

import java.util.Scanner;

public class ReverseString {
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string for reverse it: -");
        String  str = sc.nextLine();
        
        
        for(int i=str.length()-1; i>=0; i--) {
        	
        	System.out.print(str.charAt(i));
        }
        
        
                
    }

}

Output:

Reverse a string in java using Scanner


Reverse a string in java using collections word by word

First we will create an empty List of String type and add string into List.

Then using Collections.reverse() method we can reverse the list elements.

Code:

package demopkg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReverseStringCollection {

public static void main(String[] args) {
    
    List<String> list = new ArrayList();
    
    list.add("Ashish");
    list.add("Ritendra");
    list.add("Kedar");
    list.add("John");
    
    
    Collections.reverse(list);
    
    System.out.println(list);
    
    
}

}

Output: 

Reverse a string in java using collections

 

Watch Video for more clarification:


FAQ

Q1. What is reverse() in Java.

Reverse is a inbuild method of StringBuffer and StringBuilder class to reverse the string.

Syntax:

Public StringBuffer reverse()

 

Q2. What is difference between StringBuffer and StringBuilder in Java?

One main difference is that StringBuffer is synchronized but StringBuilder is not synchronized.

difference between StringBuffer and StringBuilder

 

Q3. How do you reverse a collection element?

Using Collections.reverse() method.


Summary:

I hope you enjoyed this tutorial and learnt something new.

Final word, Bookmark this post  ‘How to Reverse a String in Java’ for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

 

Must Read: Bubble Sort in Java

Leave a Comment