String reversal is changing the characters position from last to first in string.
There are different ways to to achieve reversal.
We will see them one by one.
- Using Loops (or Iterations)
- Usnig StringBuffer or StringBuilder ‘s reverse()
- Using toCharArray() of Array
- Using Recursion
- Using Collections
Lets see different ways one by one
1 Reverse a string Using Loops
Following programs will show how to use loops to reverse a string.
Using loop we extracted one one character from string using charAt() of string to extract a character and assigned to another String or StringBuffer or in StringBuilder.
This program is also helpful in String reverse in java without using inbuilt function and in reverse a string without using string function in java
Below are the simple java programs for beginners
A. Reverse a string in java using for loop
- Create a String object
String string = "Programmer";
- Create a for loop to iterate it from string object length to 0
for (int i = string.length() - 1; i>= 0; i--)
- Extract each character by using
charAt()
and assign it to another String object.
1 2 3 4 5 6 7 8 9 10 11 12 | public class StringReverse1 { public static void main(String[] args) { String string = "Programmer"; String reversedString = ""; for (int i = string.length() - 1; i >= 0; i--) { reversedString+=string.charAt(i); } System.out.println("Reversed String is " + reversedString); } } |
We run here java reverse for loop to get each characters and assigned to another string.
Printed the reversed String to get reverse string in java program.
B. Reverse a string in java using while loop
String object is inmutable so changing object value is not possible to avoid this we used StringBuffer here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class StringReverse2 { public static void main(String[] args) { String string = "Programmer"; StringBuffer reversedString = new StringBuffer(); int i = string.length()-1; while (i >= 0) { reversedString.append(string.charAt(i)); i--; } System.out.println("Reversed String is " + reversedString); } } |
C. Reverse a string in java using do while loop
Here StringBuilder is used to reverse the string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class StringReverse3 { public static void main(String[] args) { String string = "Programmer"; StringBuilder reversedString = new StringBuilder(); int i = string.length() - 1; do { reversedString.append(string.charAt(i)); i--; } while (i >= 0); System.out.println("Reversed String is " + reversedString); } } |
2. Using StringBuffer or StringBuilder classes
StringBuffer and StringBuilder provides a method reverse()
to reverse the string.
This is very simple approach any one can easily reverse string using this approach.
Using this method we can reverse string.
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Scanner; public class StringReverse4 { public static void main(String[] args) { System.out.println("Enter A string to reverse it"); Scanner scanner = new Scanner(System.in); String string = scanner.next(); String reverseString = new StringBuffer(string).reverse().toString(); System.out.println("Reversed String is " + reverseString); } } |
3. Using toCharArray() of Array
toCharArray() is used to convert String Object to character array.
We follow below steps to revers string using toCharArray().
- Take a String
- Conver it to Character array string.toCharArray();
- Reverse loop in character array
- Assing it to a string object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class StringReverse5 { public static void main(String[] args) { System.out.println("Enter A string to reverse it"); Scanner scanner = new Scanner(System.in); String string = scanner.next(); char[] characters = string.toCharArray(); String reverseString = ""; for (int i = characters.length - 1; i >= 0; i--) { reverseString += characters[i]; } System.out.println("Reversed String is " + reverseString); } } |
4. Using Recursion to reverse a String
Recursion is one of the common way to reverse a string.
In many java interviews, it also asked to reverse a string without using iteration and reverse a string in java without using inbuilt function . In that case we use recursion.
- Create a method to call itself
reverseString(String s)
- If String is empty then return
- If not empty make recursion call to reverse a string
reverseString(s.substring(1)) + s.charAt(0);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class StringReverse6 { public static String reverseString(String s) { if (s.isEmpty()) { return s; } else { return reverseString(s.substring(1)) + s.charAt(0); } } public static void main(String[] args) { System.out.println("Enter A string to reverse it"); Scanner scanner = new Scanner(System.in); String string = scanner.next(); char[] characters = string.toCharArray(); String reversedString = reverseString(string); System.out.println("Reversed String is " + reversedString); } } |
5. Using Collections to Reverse a String in Java
- Get String
- Conver to List
List charList = string.chars()
.mapToObj(e -> (char) e)
.collect(Collectors.toList());
- Reverse List
Collections.reverse(charList);
- Convert to String
String reverseString = charList.stream().map(String::valueOf).collect(Collectors.joining());
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class StringReverse7 { public static void main(String[] args) { System.out.println("Enter A string to reverse it"); Scanner scanner = new Scanner(System.in); String string = scanner.next(); char[] characters = string.toCharArray(); List<Character> charList = string.chars() .mapToObj(e -> (char) e) .collect(Collectors.toList()); Collections.reverse(charList); String reverseString = charList.stream().map(String::valueOf).collect(Collectors.joining()); System.out.println("Reversed String is " + reverseString); } } |
Above program is a good example of Java 8 using Streams