How to Reverse a String in Java is one of the popular interview questions, but the interviewer might add some twist to it by asking you to write the code without using the reverse() method, recursion, etc. In this article, we will learn the possible ways of reversing a string in Java. We will look into techniques of reversing a single word and group of words in a sentence [Word by Word]
Java Reverse String
Method 1: Using reverse() function — Using StringBuilder (or) StringBuffer
This is the easiest way of reversing a String, we can use reverse() method of either StringBuilder (or) StringBuffer.
Note: StringBuilder can give a better performance as it is not Synchronized. StringBuffer is Synchronized
import java.util.Scanner; public class StringReverse { public static void main(String[] args) { String reverseString = ""; System.out.println("Enter string to reversed"); //Read the input from the user Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); //Pass input to constructor of StringBuilder StringBuilder inputStr = new StringBuilder(input); //Use reverse() method to reverse the String reverseString = inputStr.reverse().toString(); System.out.println("Original String : "+input); System.out.println("Reversed String : "+reverseString); } }
-
- Get the input string from the user and pass it to the constructor of the StringBuilder.
- Use the reverse() method of the StringBuilder class to get the reversed String.
Output:
Method 2: Reverse String Using charAt()
import java.util.Scanner; public class StringReverse { public static void main(String[] args) { String reverseString = ""; System.out.println("Enter string to reversed"); //Read the input from the user Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); //Convert input to inputArray using toCharArray() char[] inputArray = input.toCharArray(); for(int i=inputArray.length-1;i>=0;i--) { reverseString = reverseString+inputArray[i]; } System.out.println("Original String : "+input); System.out.println("Reversed String : "+reverseString); } }
- Get the input string from the user and convert the input string into a character array inputArray using toCharArray() method
- Iterate the inputArray from end to start, each time append it to the reverseString.
Output:
Enter string to reversed JavaInterviewPoint Original String : JavaInterviewPoint Reversed String : tnioPweivretnIavaJ
Method 3: using Recursion
package com.javainterviewpoint; import java.util.Scanner; public class StringReverse { public static void main(String[] args) { String reversedString = ""; System.out.println("Enter string to reversed"); //Read the input from the user Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); reversedString = reverseString(input); System.out.println("Original String : "+input); System.out.println("Reversed String : "+reversedString); } public static String reverseString(String input) { if(input.isEmpty()) return input; //Call reverseString() function recursively return reverseString(input.substring(1)) + input.charAt(0); } }
- Get the input string from the user and pass the input string to the reverseString() method.
- In the reverseString() method, we will be recursively subString() the first character of the input String and append it to the end using charAt() method.
Method 4: Using Stack
import java.util.Scanner; import java.util.Stack; public class StringReverse { public static void main(String[] args) { StringBuilder reverseString = new StringBuilder(); System.out.println("Enter string to reversed"); // Read the input from the user Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); // Create a stack of characters Stack<Character> stack = new Stack<Character>(); // Push each character into the stack for (int i = 0; i < input.length(); i++) { stack.push(input.charAt(i)); } // pop each characters from the stack until it is empty while (!stack.empty()) { reverseString.append(stack.pop()); } System.out.println("Original String : "+input); System.out.println("Reversed String : "+reverseString); } }
- Get the input string from the user.
- Create a Stack of Character and push each character of the input string into the stack.
- Pop each character from the stack until it is empty and append it to the reverseString.
Method 5: Using Collections reverse() method
- Get the input string from the user
- Create a List of Character (characterList) and push each character of the input string into the characterList.
- Use Collections.reverse() method to reverse the elements of the characterList
- Iterate the characterList from end to start, each time append it to the reverseString
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class StringReverse { public static void main(String[] args) { StringBuilder reverseString = new StringBuilder(); System.out.println("Enter string to reversed"); // Read the input from the user Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); //Create a list of characters List<Character> characterList = new ArrayList<Character>(); //Push each characters into the characterList for(Character c : input.toCharArray()) { characterList.add(c); } //Reverse the List using Collections.reverse() Collections.reverse(characterList); //Convert ArrayList to String for(Character c : characterList) { reverseString.append(c); } System.out.println("Original String : "+input); System.out.println("Reversed String : "+reverseString); } }
Java Reverse String Word by Word
All the above methods will work good for a single word, lets now learn how to reverse a string in Java word by word,
We will reverse each word in a sentence.
Method 1: Using StringBuffer
- Get the input string from the user
- Using split() method split the sentence into words and save them to a String array (words)
- Iterate through the String array and use reverse() method of the StringBuffer class to reverse the String and keep appending the reversedString.
package com.javainterviewpoint; import java.util.Scanner; public class StringReverse { public static void main(String[] args) { String reverseString = ""; System.out.println("Enter string to reversed"); //Read the input from the user Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); // Split sentence into seperate words String[] words = input.split(" "); // Iterate the String array for(String word : words) { // Append each reversed Words reverseString = reverseString + new StringBuilder(word).reverse().toString()+" "; } System.out.println("Original String : "+input); System.out.println("Reversed String : "+reverseString); } }
Output:
Enter string to reversed Hello World Original String : Hello World Reversed String : olleH dlroW
Method 2: Using charAt()
- Get the input string from the user
- Using split() method split the sentence into words and save them to a String array (words)
- Iterate through the String array and convert each word into a character array using toCharArray() method
- Iterate through the character array in the decreasing order and each time append the character to the reverseString
package com.javainterviewpoint; import java.util.Scanner; public class StringReverse { public static void main(String[] args) { String reverseString = ""; System.out.println("Enter string to reversed"); //Read the input from the user Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); // Split sentence into seperate words String[] words = input.split(" "); // Iterate the String array for(String word : words) { //Convert input to inputArray using toCharArray() char[] inputArray = word.toCharArray(); for(int i=inputArray.length-1; i>=0; i--) { reverseString = reverseString+inputArray[i]; } reverseString = reverseString + " "; } System.out.println("Original String : "+input); System.out.println("Reversed String : "+reverseString); } }
Yogesh Mangde says
I like it! great explanation and different solutions for the same problem.