Reverse a string in Java program with 6 Different Ways

By | July 12, 2018

In this post of Reverse a string in Java program with 6 Different Ways We will learn how to reverse a String in java using in-built API and own custom logic.

There are several ways to solve the programming problem. Library classes may be very useful in solving programming problems. If you do not know these classes and methods, then you will spend time in writing your own methods and wasting time.

We will use the programming problem to return a reverse of the given string in various ways in this post.

If you run any of the client program that will ask you to enter string which you want to reverse as below:

I have tested these programs for input string “JavaTutorials”  and I got Reversed String: “slairotuTavaJ” output.
You can test these programs for various input string.

Approach 1:

This approach using non-recursive solution. It follows the logic of starting from the end of the String and keep moving back character by character and keep appending those characters to form a new string which is reverse of the given input String.

Approach 2:

This approach also using non-recursive solution Here first of all we converted input string into character  array using toCharArray() method of String class. After that It follows the logic of starting from the end of the character array and keep moving back character by character and keep appending those characters to form a new string which is reverse of the given input String.

Approach 3:

This approach can be described as below steps:

Step 1. First of all convert input string into array of bytes as below

byte[] bytes = inputText.getBytes();

Step 2. Create a temporary byte [] array of length equal to length of the input string or length of bytes array(step1).

byte []revByteArr = new byte[bytes.length];

Step 3. Store the bytes (from step1) in reverse order into temporary byte []revByteArr array which we created in step 2

Step 3. Finally create a new String Object with byte revByteArr[] to store the result.

Approach 4:

In this approach first of all Convert the input string into character array by using the toCharArray() of String class after that scan the character array from both sides Like from the starting index (left) as well as from last index(right) simultaneously.

  1. First set the left index equal to 0 and right index equal to the length of the string -1.
  2. Now swap the characters of the start index scanned with the last index scanned one by one. Afterwards that, increase the left index by 1 (left++) and you to decrease the right by 1 i.e. (right–) to move on to the next characters in the character array.
  3. This process has to continue till left index is less than or equal to the right index.

Approach 5:

In this approach we have passed input string to StringBuffer constructor in that way we have converted input string into StringBuffer and finally we have called reverse() method of StringBuffer class.

If you are not concern about thread safety then you may use StringBuilder(this class also has reverse() method) instead of StringBuffer class

Approach 6:

In this approach we will create a function to reverse a string  input taken by user and later we will call it recursively until all characters are reversed.

Would you like to watch on YouTube:

Reverse a string in Java program

That’s all about  how to reverse a string in Java.
If you have any feedback or suggestion please feel free to drop in blow comment box.

One thought on “Reverse a string in Java program with 6 Different Ways

Leave a Reply

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