Reverse a String using Stack in Java
This post will discuss how to reverse a string using Stack in Java.
The following example demonstrates how to reverse a string with a Stack data structure in Java. Following are the complete steps:
- Create an empty stack of characters.
- Convert given string into character array using
String.toCharArray()method and push each character of it into the stack. - 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.
- Finally, convert the character array into string using
String.copyValueOf(char[])and return the formed string.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.Stack; class Main { // Method to reverse a string in Java using a stack and character array public static String reverse(String str) { // base case: if the string is null or empty if (str == null || str.equals("")) { return str; } // create an empty stack of characters Stack<Character> stack = new Stack<Character>(); // push every character of the given string into the stack char[] ch = str.toCharArray(); for (int i = 0; i < str.length(); i++) { stack.push(ch[i]); } // start from index 0 int k = 0; // pop characters from the stack until it is empty while (!stack.isEmpty()) { // assign each popped character back to the character array ch[k++] = stack.pop(); } // convert the character array into a string and return it return String.copyValueOf(ch); } public static void main(String[] args) { String str = "Reverse me!"; str = reverse(str); // string is immutable System.out.println("The reversed string is " + str); } } |
Output:
The reversed string is !em esreveR
We can also use StringBuilder instead of a character array, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.util.Stack; class Main { // Method to reverse a string in Java using a stack and StringBuilder public static String reverse(String str) { // base case: if the string is null or empty if (str == null || str.equals("")) { return str; } // create an empty stack of characters Stack<Character> stack = new Stack<Character>(); // push every character of the given string into the stack for (int i = 0; i < str.length(); i++) { stack.push(str.charAt(i)); } // pop characters from the stack and append them into StringBuilder StringBuilder sb = new StringBuilder(); while (!stack.isEmpty()) { sb.append(stack.pop()); } // convert `StringBuilder` to string and return return sb.toString(); } public static void main(String[] args) { String str = "Reverse me!"; str = reverse(str); // string is immutable System.out.println("The reversed string is " + str); } } |
Output:
The reversed string is !em esreveR
That’s all about reversing a String using Stack in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)