Wednesday, 7 October 2015

Java program to reverse the order of the string

Requirement: When I run the Java program, it should ask me to enter one string value. And as an output it should display the supplied string in the reverse order (say if the supplied string is Susanto it should give the output as otnasuS).

Solution: The Java program for the above requirement is shown below:

Program 1:

package com.susanto;

import java.util.Scanner;

public class ReverseString {

    public static void main(String[] args) {
        String stringToReverse, reversedString = "";
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string to reverse : ");
        stringToReverse = in.nextLine();
        for (int i = stringToReverse.length() - 1; i >= 0; i--) {
            reversedString = reversedString + stringToReverse.charAt(i);

        }
        System.out.println("Thus the reversedString is : " + reversedString);
    }
}

Output:

Enter a string to reverse : Susanto
Thus the reversedString is : otnasuS

It would be better if reversedString was an instance of StringBuffer in the above program. So, we could use method append rather than + operator. Our code in the above Program 1 is instantiating unnecessary Strings inside the for loop. Remember that Java Strings are immutable.

Program 2:
package com.susanto;

import java.util.Scanner;

public class ReverseString {

      public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter a string to reverse : ");
            String stringToReverse = in.nextLine();
            StringBuffer reversedString = new StringBuffer();
            for (int i = stringToReverse.length() - 1; i >= 0; i--) {
            //StringBuffer is mutable, using append prevents from                      //instantiating partial strings
                  reversedString.append(stringToReverse.charAt(i));
            }
            System.out.println("Thus the reversedString is : "
                        + reversedString.toString());
      }
}
Output:
Enter a string to reverse : Susanto
Thus the reversedString is : otnasuS

We can also improve the above code in Program 2 by replacing StringBuffer class with StringBuilder class because "reversedString" is local variable, so it is already thread safe as shown in Program 3 below:

Program 3:

package com.susanto;

import java.util.Scanner;

public class ReverseString {

      public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter a string to reverse : ");
            String stringToReverse = in.nextLine();
            StringBuilder reversedString = new StringBuilder();
            for (int i = stringToReverse.length() - 1; i >= 0; i--) {
                  reversedString.append(stringToReverse.charAt(i));
            }
            System.out.println("Thus the reversedString is : "
                        + reversedString.toString());
      }
}

Output:

Enter a string to reverse : Moumita
Thus the reversedString is : atimuoM



Thanks & Regards,
Susanto Paul