Java reverse String Example
In this example, we are going to see how to reverse a String in Java. We are going to see how to this, very conveniently using a StringBuffer
, and two other solutions using recursion and simply using a character array.
Let’s see the examples.
1. Reversing a String using StringBuffer
Let’s see how you can use a StringBuffer
to reverse a String
in a single line, using its reverse
API method:
StringReverseExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 | package com.javacodegeeks.core.string; public class StringReverseExample { public static void main(String[] args) { String str = "This was a String to reverse" ; String reverse = new StringBuffer(str).reverse().toString(); System.out.println( "Normal String is : " + str + " \nReverse String is : " +reverse); } } |
This will output:
Normal String is : This was a String to reverse
Reverse String is : esrever ot gnirtS a saw sihT
This is the easiest way you can reverse a String
in Java.
2. Reversing a String using iteration through characters
In this case we are first going to obtain a character array from the String
in order to handle its characters individually. Then, we can either choose to create another array of the same length and simply copy the last character of the first array to first character of the second, then the second to last character of the first array to the second character of the second e.t.c. Another way would be to swap the characters in place in the same array.
Let’s see how:
StringReverseExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.javacodegeeks.core.string; public class StringReverseExample { public static void main(String[] args) { String str = "This was a String to reverse" ; char [] characters = str.toCharArray(); char [] reverseChars = new char [characters.length]; for ( int i = 0 ; i < characters.length ; i++){ reverseChars[i] = characters[(characters.length- 1 )-i]; } String reverse = new String(reverseChars); System.out.println( "Normal String is : " + str + " \nReverse String is : " +reverse); // If you don't want to use double the memory // swap the characters in place for ( int i = 0 ; i < characters.length/ 2 ; i++){ char t = characters[i]; characters[i] = characters[(characters.length- 1 )-i]; characters[(characters.length- 1 )-i] = t; } System.out.println( "Normal String is : " + str + " \nReverse String is : " +String.valueOf(characters)); } } |
This will output:
Normal String is : This was a String to reverse
Reverse String is : esrever ot gnirtS a saw sihT
Normal String is : This was a String to reverse
Reverse String is : esrever ot gnirtS a saw sihT
3. Reversing a String using recursion
This is how you can use recursion to compute the reverse of String:
StringReverseExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 | package com.javacodegeeks.core.string; public class StringReverseExample { public static void main(String[] args) { String str = "Java" ; String reverseStr = reverseRecurive(str); System.out.println( "Normal String is : " + str + " \nReverse String is : " +reverseStr); } public static String reverseRecurive(String str) { if (str.length() <= 1 ) { return str; } return reverseRecurive(str.substring( 1 )) + str.charAt( 0 ); } } |
This will output:
Normal String is : Java
Reverse String is : avaJ
At first sight, the above recursive algorithm, might not be straight forward. Let’s see how this works. We can model the recursive calls like this
- 1st call –
reverseRecurive("Java")
: will returnreverseRecurive("ava") + "J"
- 2nd call –
reverseRecurive("ava")
: will returnreverseRecurive("va") + "a"
- 3rd call –
reverseRecurive("va")
: will returnreverseRecurive("a") + "v"
- 4th call –
reverseRecurive("a")
: will return"a"
- 3rd call – will return :
reverseRecurive("a")
+ “v” = “a” + “v” - 2nd call – will return :
reverseRecurive("va")
+ “a” = “a” + “v” + “a” - 1st call – will return :
reverseRecurive("ava")
+ “J” = “a” + “v” + “a” + “J”
That should make it a bit more clear. As you can see, in this example the depth of the recursion is equal to the length of the String. If the String
is very long and stack size is of major concern, Divide and Conquer is the way to go:
StringReverseExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.javacodegeeks.core.string; public class StringReverseExample { public static void main(String[] args) { String str = "Hello World" ; String reverseStr = reverseRecursiveDivide(str); System.out.println( "Normal String is : " + str + " \nReverse String is : " + reverseStr); } public static String reverseRecursiveDivide(String str) { if (str.length() <= 1 ) { return str; } return reverseRecursiveDivide(str.substring(str.length() / 2 , str.length())) + reverseRecursiveDivide(str.substring( 0 , str.length() / 2 )); } } |
This will output:
Normal String is : Hello World
Reverse String is : dlroW olleH
The logic here is that you take the first half of the String
, you reverse it, take the other half of the String
and reverse it, and finally concatenate them with the reverse order ( reversed second half + reversed first half). And that’s it.
To sum up, there is no particular reason why you shouldn’t use the simple first method with StringBuffer
. But if you don’t want to do that, for example in an interview question, as you can see there are plenty of fast solutions.
4. Download Source Code
This was a Java String reverse Example.
You can download the source code of this example here: Java reverse String Example
Last updated on Apr. 08th, 2020