Reverse a String in Java in 10 different ways

In this post, we will see how to reverse a string in Java in 10 different ways. For example, reverse of “Techie Delight” is “thgileD eihceT”.

 


We have covered 10 different ways (and 15 different implementations) to reverse a string in Java –

  1. Using StringBuilder/StringBuffer
  2. Using Stack
  3. Using Java Collections Framework reverse() method
  4. Using character array
  5. Using character array and swap()
  6. Using + (string concatenation) operator
  7. Using Unicode Right-to-left override (RLO) character
  8. Using Byte Array
  9. Using Recursion
  10. Using substring() function

 


 

1. Using StringBuilder/StringBuffer

We can use StringBuilder.reverse() method to efficiently reverse a string in Java. Alternativey, we can also use StringBuffer.reverse() method. Using StringBuilder is suggested as it’s not synchronized and more faster than StringBuffer.

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

2. Using Stack

We can take help of Stack data structure to reverse a string in Java. Below are the complete steps –

  1. Create an empty stack of characters.
     
  2. Convert given String into character array using String.toCharArray() function and push each character of it into the stack.
     
  3. Remove characters from the stack until it becomes empty and assign them back to the character array. As stack follows FILO order, characters will be inserted in the reverse order.
     
  4. Finally, convert the character array into string using String.copyValueOf(char[]) and return the formed string.
     

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 
We can also use StringBuilder instead of character array as shown below –

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

3. Using Java Collections Framework reverse() method

We can use Collections.reverse() to reverse a string in Java. Below are the complete steps –

  1. Create an empty ArrayList of characters and initialize it with characters of the given string using String.toCharArray().
     
  2. Reverse the list using java.util.Collections reverse() method.
     
  3. Finally, convert the ArrayList into String using StringBuilder and return the formed string.
     

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 
Above solution first converts ArrayList into StringBuilder, then finally StringBuilder into a String. We can directly convert ArrayList into a String by removing square brackets, commas and single space from it. Below code uses String.replaceAll() to achieve that. But this approach is highly inefficient and should be avoided at all costs.

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

4. Using character array

We know that we cannot make any change in the String object as String is immutable in Java. But we can use a character array that can be modified easily –

  1. Create an empty character array of same size as that of given String.
     
  2. Fill the character array backwards with characters of the given string.
     
  3. Finally, convert the character array into string using String.copyValueOf(char[]) and return it.
     

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

5. Using character array and swap()

Below is another efficient way to reverse a string in Java using character array –

  1. Create a character array and initialize it with characters of given string using String.toCharArray().
     
  2. Start from the two end points l and h of the given string. Run the loop till two end-points intersect (l <= h). In each iteration of the loop, swap values present at indexes l & h and increment l & decrement h.
     
  3. Finally, convert the character array into string using String.copyValueOf(char[]) and return.
     

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

6. Using + (string concatenation) operator

We can make use of String Concatenation operator + to reverse a string in Java by reading characters from the end of it and concatenating in the beginning of a new string. Please note that to increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression[1].

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

7. Using Unicode Right-to-left override (RLO) character

We can use Unicode Right-to-left override (RLO) character to convert string into its reverse as shown below -

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

8. Using Byte Array

The idea is very simple. We convert the given string into bytes and then inplace rearranges the byte array as shown in Approach #5. Finally, we convert the byte array back into the string.

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

9. Using Recursion

As seen in the Approach #2, we can easily reverse a string in Java using stack data structure. As stack is involved, we can easily convert the code to use recursion call stack. As String is immutable, we first convert the given String into character array, then reverse the character array and finally convert the character array back into the string.

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 
Above solution uses a static variable which is not recommended. We can easily solve this problem without using any static variable. This approach is almost similar to the Approach #3 discussed here.

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

10. Using substring() function

We can use String.substring(int, int) method to recursively reverse a string in Java. Below code uses String.charAt(int) method to isolate the first or last character of the String and recurse for remaining string using substring().

Approach #1

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 

Approach #2

 

Download   Run Code

Output:

Reverse of the given string is : thgileD eihceT

 
References: String Javadoc SE 8

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
Sort by:   newest | oldest | most voted
mike
Guest
mike

This is great guys… thank you!

wpDiscuz