Java Program To Reverse a String

Java Program To Reverse a String

Java Program To Reverse a String

There are many ways to reverse a string in Java. A string can be reversed using StringBuffer, StringBuilder, recursion and using user defined function. So, today we will discuss various methods to reverse value of String variable in Java. Before starting I recommend you to also check how to reverse a number In Java.

Reverse String Using StringBuffer

public class strRev {
    public static void main(String args[]) {

        String strVar = "Code Nirvana Technology Blog";
        
        /* String Reverse Using StringBuffer */
        String revStr = new StringBuffer( strVar ).reverse().toString();
        
        System.out.println( "Before: " + strVar );
        System.out.println( "After: " + revStr );

    }
}

Reverse String Using StringBuilder

public class strRev {
    public static void main(String args[]) {

        String strVar = "Code Nirvana Technology Blog";
        
        /* String Reverse Using StringBuilder */
        String revStr = new StringBuilder( strVar ).reverse().toString();
        
        System.out.println( "Before: " + strVar );
        System.out.println( "After: " + revStr );

    }
}

Reverse String Using User Defined Function

public class strRev {
    public static void main(String args[]) {

        String strVar = "Code Nirvana Technology Blog";
        
        /* String Reverse Using User Defined Function */
        String revStr = revString( strVar );
        
        System.out.println( "Before: " + strVar );
        System.out.println( "After: " + revStr );

    }
    
    public static String revString(String str){
        String rev = "";
        for( int i = str.length()-1; i>=0; i-- ){
            rev = rev + str.charAt(i);
        }
        return rev;
    }
}

Reverse String Using Recursion

public class strRev {
    public static void main(String args[]) {

        String strVar = "Code Nirvana Technology Blog";
        
        /* String Reverse Using Recursion */
        String revStr = revString( strVar );
        
        System.out.println( "Before: " + strVar );
        System.out.println( "After: " + revStr );

    }
    
    public static String revString(String str) {
        if (str.length()<2) {
            return str; 
        }
        return revString(str.substring(1)) + str.charAt(0);
    }
}
More About Recursion: Programs Using Recursion

Output
Output: Java Program To Reverse a String

All In One Java Program To Reverse a String

//Java Program To Reverse a String
//Author: http://www.codenirvana.in/

public class StringReverse {

    public static void main(String args[]) {
        
        String input = "Java Programming is Awesome";
        System.out.println( "Input: " + input + "\n" );
        
        /* String Reverse Using StringBuffer */
        String rev1 = new StringBuffer( input ).reverse().toString();
        
        /* String Reverse Using StringBuilder */
        String rev2 = new StringBuilder( input ).reverse().toString();
        
        /* String Reverse Using User Defined Function */
        String rev3 = revString( input );
        
        /* String Reverse Using Recursion */
        String rev4 = revString( input );
        
        //Print rev variables
        System.out.println( "StringBuffer: " + rev1 );
        System.out.println( "StringBuilder: " + rev2 );
        System.out.println( "Function: " + rev3 );
        System.out.println( "Recursion: " + rev4 );
        
    }
    //String Reverse Function
    public static String revString(String str){
        String rev = "";
        for( int i = str.length()-1; i>=0; i-- ){
            rev = rev + str.charAt(i);
        }
        return rev;
    }
    //String Reverse Recursion Function
    public static String revStrRecursion(String str) {
        if (str.length()<2) {
            return str; 
        }
        return revString(str.substring(1)) + str.charAt(0);
    }
}

Conclusion
Now you know four methods to reverse a string in Java and its one of the famous interview question on Java Programming i.e, What are the various ways you can apply to reverse a sting in Java...

Having problem? Leave your comment below and we will solve your queries as soon as possible. Don't forget to like and share this program and Subscribe to our newsletter and get free updates of latest programming articles.

5 Comments Leave new

Write a program in java to take an integer input from the user. Programmer should apply a check, such that the input should not be less than 4 digits. After this program should verify that whether the input is prime or not with the help of recursion. If the number is prime, then display the factorial of sum of its digits using recursion, otherwise display the reverse of the number .

Reply

Write a program in java to count the occurrences of “stars” string in the following string:
“The world is full of stars. The stars are very shining and bright. Sun is the biggest star in this universe”.

Reply

Wirte a program to print following:
input- I Love Java
output- Java Love I

Reply

i want a program which will print the following series :-
1+2/2!+3/3!.....n/n!
its one plus two by two factorial ..
please help me as soon as possible

Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.