Write a java program to reverse a string?

1) Using StringBuffer class

In this method, we use reverse() method of StringBuffer class to reverse the string. Here is the code snippet to reverse the string using reverse() method of StringBuffer class.

StringBuffer sbf = new StringBuffer("MyJava");
 
System.out.println(sbf.reverse());    //Output : avaJyM

2) Using iterative method

In this method, first we convert given string to char array using charArray() method. And then we iterate that array in the reverse order.

String str = "MyJava";
 
char[] strArray = str.toCharArray();
 
for (int i = strArray.length - 1; i >= 0; i--)
{
    System.out.print(strArray[i]);     //Output : avaJyM
}

3) Using recursive method.

Here is the method which reverses the string by calling itself recursively.

static String recursiveMethod(String str)
{
     if ((null == str) || (str.length() <= 1))
     {
            return str;
     }
 
     return recursiveMethod(str.substring(1)) + str.charAt(0);
}

This method takes the first character of a string (str.charAt(0)) and puts it at the end of the string. And then calls itself on the remainder of the string (str.substring(1)). Finally adds these two things to get the reverse of the passed string (recursiveMethod(str.substring(1)) + str.charAt(0)). When the passed string is one character or less (str.length() <= 1), it stops calling itself and just returns the string passed.

If the “MyJava” is the string to reverse, then this method works like this.

1st Call —>   recursiveMethod(“MyJava”)
2nd Call —> recursiveMethod(“yJava”) + “M”
3rd Call —>  (recursiveMethod(“Java”) + “y”) + “M”
4th call —>  ((recursiveMethod(“ava”) + “J”)+”y”) + “M”
5th Call —>  (((recursiveMethod(“va”) + “a”) + “J”)+”y”) + “M”
6th Call —>  ((((recursiveMethod(“a”) + “v”) + “a”) + “J”)+”y”) + “M”

After 6th call, it Stops calling itself. Because the length of passed string is 1. So, finally it returns “avaJyM”.

Below is the Java program which reverses the string “MyJava” using all three above methods.

public class ReverseTheString
{
    public static void main(String[] args)
    {
        String str = "MyJava";
 
        //1. Using StringBuffer Class
 
        StringBuffer sbf = new StringBuffer(str);
 
        System.out.println(sbf.reverse());    //Output : avaJyM
 
        //2. Using iterative method
 
        char[] strArray = str.toCharArray();
 
        for (int i = strArray.length - 1; i >= 0; i--)
        {
            System.out.print(strArray[i]);    //Output : avaJyM
        }
 
        System.out.println();
 
        //3. Using Recursive Method
 
        System.out.println(recursiveMethod(str));    //Output : avaJyM
    }
 
    //Recursive method to reverse string
 
    static String recursiveMethod(String str)
    {
         if ((null == str) || (str.length() <= 1))
         {
                return str;
         }
 
         return recursiveMethod(str.substring(1)) + str.charAt(0);
    }
}

 

Share

Recent Posts

How to reverse Singly Linked List?

[crayon-662d739966c32623314762/] [crayon-662d739966c3f054667342/] Output: Adding: 3 Adding: 32 Adding: 54 Adding: 89 3 32 54 89…

5 years ago

Find out duplicate number between 1 to N numbers.

[crayon-662d739966dab503770880/]

5 years ago

Find out middle index where sum of both ends are equal

You are given an array of numbers. Find out the array index or position where…

5 years ago

Write a singleton class in Java

Singleton class means you can create only one object for the given class. You can…

5 years ago

Write a java program to remove all white spaces from a string.?

1) Using replaceAll() Method. In the first method, we use replaceAll() method of String class…

5 years ago

Singly linked list implementation

Singly Linked Lists are a type of data structure. It is a type of list.…

5 years ago