How To Reverse a String In Java Using reverse() Function

How To Reverse A String In Java Using Reverse Function | In Java, the String class contains lots of predefined methods for string manipulation but it doesn’t contain any method to reverse the string data. Similar to the String class, the StringBuilder and StringBuffer classes also contain many predefined methods for string manipulation. Both classes contain a reverse() method to reverse the given StringBuffer/StringBuilder object. Also see:- How To Reverse a String In Java

The StringBuffer class is mostly used for Multi-thread environments because all the methods of the StringBuffer class are synchronized, whereas the StringBuilder class is used in single-thread applications. Hence we will prefer the StringBuilder object. Since the reverse() method is present in these classes therefore we have to convert string data to StringBuilder/StringBuffer objects.

Method prototype in StringBuilder class:-
public StringBuilder reverse()

Method prototype in StringBuffer class:-
public synchronized StringBuffer reverse()

Now let us see how to reverse a string in Java using the reverse function

public class Main {
   public static void main(String args[]) {
      String string =  "Know Program";

      // convert string to StringBuilder
      StringBuilder sb = new StringBuilder(string);

      // call reverse() function
      sb.reverse();
      
      // store result to String by
      // converting StringBuilder to String
      String reverse = sb.toString();
      
      // display strings
      System.out.println("Original String: " + string);
      System.out.println("Reversed String: " + reverse);
   }
}

Output:-

Original String: Know Program
Reversed String: margorP wonK

In the above program, we have initialized a string variable with the string literal “Know Program”. After that, we have converted the string to a StringBuilder object. To reverse the StringBuilder object, we have called the reverse() method. Now, again we have converted the StringBuilder object to a string object using the toString() method. 

How To Reverse a String In Java Using reverse() Function by taking Input from the User 

Here we will see how to reverse a string In Java using the reverse() function by taking input from the user. To take input from the user, we will take the help of the Scanner class.

import java.util.Scanner;

public class Main {
   public static void main(String args[]) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a String: ");
      String string = scan.nextLine();

      String reverse = 
         new StringBuilder(string).reverse().toString();
      System.out.println("Reversed String: " + reverse);
      scan.close();
   }
}

Output:-

Enter a String: Java Programming
Reversed String: gnimmargorP avaJ

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *