- Push the character one by one into the Stack of datatype character.
- Pop the character one by one from the Stack until the stack becomes empty.
- Add a popped element to the character array.
- Convert character array to string.
- Return reversed string.
Below is the implementation of the above approach.
GeeksForGeeks <- Reverse -> skeeGroFskeeG Hello World <- Reverse -> dlroW olleH
Time Complexity: O(n), where n is a number of characters in the stack.
Auxiliary Space: O(n) for the stack.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Stack | Set 3 (Reverse a string using stack)
- Program to reverse a linked list using Stack
- Reverse the Words of a String using Stack
- Python program to reverse a stack
- Sort a stack using a temporary stack
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- Reverse a number using stack
- Reverse a stack without using extra space in O(n)
- Print Reverse a linked list using Stack
- Reverse a stack using recursion
- Stack Permutations (Check if an array is stack permutation of other)
- Java Program to Find Reverse of a Number Using Recursion
- Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop
- Java Program to Reverse a Sentence Using Recursion
- Java Program to Print Stack Trace
- Sort string of characters using Stack
- Remove all duplicate adjacent characters from a string using Stack
- Java Program for Reverse a linked list
- Java Program to Reverse a Number & Check if it is a Palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.