String Reverse String using Stack
- Initialize an empty stack.
- Push each character of the string onto the stack.
- Pop the characters from the stack and append them to a new string.
- The new string is the reversed version of the original string.
Below is the implementation of the above approach:
C++
// C++ program to reverse a string using stack #include <bits/stdc++.h> using namespace std; void reversebyStack(string &str) { // To store the characters of original string. stack< char > st; for ( int i=0; i<str.length(); i++) // Push the charcters into stack st.push(str[i]); for ( int i=0; i<str.length(); i++) { // Pop the charcters of stack into the original string. str[i] = st.top(); st.pop(); } } // Driver program int main() { // Original String string str = "w3wiki" ; reversebyStack(str); cout << str; return 0; } |
Java
// Java program to reverse a string using stack import java.util.*; class GFG { public static String reversebyStack( char []str) { Stack<Character> st = new Stack<>(); for ( int i= 0 ; i<str.length; i++) // Push the charcters into stack st.push(str[i]); for ( int i= 0 ; i<str.length; i++) { // Pop the charcters of stack into the original string. str[i] = st.peek(); st.pop(); } return String.valueOf(str); // converting character array to string } // Driver program public static void main(String []args) { String str = "w3wiki" ; str = reversebyStack(str.toCharArray()); // passing character array as parameter System.out.println(str); } } // This code is contributed by Adarsh_Verma |
Python3
# Python program to reverse a string using stack def reversebyStack( str ): # using as stack stack = [] for i in range ( len ( str )): # Push the charcters into stack stack.append( str [i]) for i in range ( len ( str )): # Pop the charcters of stack into the original string. str [i] = stack.pop() if __name__ = = "__main__" : str = "w3wiki" # converting string to list # because strings do not support # item assignment str = list ( str ) reversebyStack( str ) # converting list to string str = ''.join( str ) print ( str ) # This code is contributed by # sanjeev2552 |
C#
// C# program to reverse a string using stack using System; using System.Collections.Generic; class GFG { public static String reversebyStack( char []str) { Stack< char > st = new Stack< char >(); for ( int i = 0; i < str.Length; i++) // Push the charcters into stack st.Push(str[i]); for ( int i = 0; i < str.Length; i++) { // Pop the charcters of stack into the original string. str[i] = st.Peek(); st.Pop(); } // converting character array to string return String.Join( "" ,str); } // Driver program public static void Main() { String str = "w3wiki" ; // passing character array as parameter str = reversebyStack(str.ToCharArray()); Console.WriteLine(str); } } // This code is contributed by Rajput-Ji |
Javascript
// Function to reverse a string using a stack function reverseByStack(str) { // To store the characters of the original string. const stack = []; // Push the characters into the stack. for (let i = 0; i < str.length; i++) { stack.push(str[i]); } // Pop the characters of the stack and build the reversed string. let reversedStr = '' ; while (stack.length > 0) { reversedStr += stack.pop(); } return reversedStr; } // Driver program function main() { // Original String let str = "w3wiki" ; let reversedStr = reverseByStack(str); console.log(reversedStr); } main(); // This code is contributed by shivamgupta0987654321 |
skeegrofskeeg
Time Complexity: O(n)
Auxiliary Space: O(n)
String Reverse in C/C++/Java/Python/JavaScript
String reverse or reverse a string means changing the position of each character of the given string to its opposite position from end, i.e. if a character is at position 1 then its new position will be String.length, similarly if a character is at position 2 then its new position will be String.length – 1, and so on.
Given a string, write a program to reverse the string.
Input: original_string[] = Geeks
Output: string_reversed[] = skeeGInput: original_string[] = w3wiki
Output: string_reversed[] = skeeGroFskeeG
Table of Content
- Reverse String using a Loop:
- Reverse String using inbuilt method
- Reverse String using Recursion:
- Reverse String using two pointers:
- String Reverse String using Stack: