Reverse a string using Stack | Practice | GeeksforGeeks

You are given a string S, the task is to reverse the string using stack.
 
Example 1:

Input: S="GeeksforGeeks"
Output: skeeGrofskeeG
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function reverse() which takes the string S as an input parameter and returns the reversed string.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ length of the string ≤ 100
 
0
itmithu14318 hours ago
C++ Solution
//0.01 second 
//return the address of the stringchar* reverse(char *S, int len){   //code here   stack<char> st;   for(int i=0; i<len; i++){       st.push(S[i]);   }   int i=0;   while (!st.empty()) {       S[i++] =st.top();       st.pop();   }   return S;}
0
dsssoni6 days ago
JAVA Solution : 
 
public String reverse(String S){              Stack<Character> st = new Stack<Character>();       StringBuilder reversed = new StringBuilder();              for(int i=0; i<S.length(); i++){           st.push(S.charAt(i));       }              int i = 0;       while(i<st.size()){           reversed.append(st.pop());       }              return reversed.toString();   }
+1
hrithikjain98891 week ago
//return the address of the stringchar* reverse(char *s, int len){   //code here   stack<char> ss;          for(int i=0;i<len;i++)   ss.push(s[i]);   int i=0;   while(ss.size()!=0)   {       s[i++]=ss.top();       ss.pop();   }   return s;}
0
paritoshpranjal32 weeks ago
class Solution {      public String reverse(String S){          Stack<Character>  st=new Stack<>();      char arr[]=new char[100];              for(int i=0;i<S.length();i++)       {           st.push(S.charAt(i));       }             for(int i=0;i<S.length();i++)       {           arr[i]= st.pop();       }        String string = String.valueOf(arr);       return string;                 }
}
0
mayank180919992 weeks ago
char* reverse(char *S, int len)
{
    //code here
    stack<int>st;
    for(int i=0;i<len;i++){
        st.push(S[i]);
    }
    int i=0;
    while(!st.empty()){
        S[i++]=st.top();
        st.pop();
    }
    return S;
}
0
hharshit81182 weeks ago
char* reverse(char *S, int len){   stack<char> s;   for(int i = 0; i< len; i++){       s.push(S[i]);   }   int index = 0;   while(!s.empty()){       S[index++] = s.top(); s.pop();   }   return S;}
0
ashishpatel39462 weeks ago
simple java solution
class Solution {      public String reverse(String S){    Stack <Character> s = new Stack<>();    for(int i = 0; i<S.length(); i++){        s.push(S.charAt(i));    }String str = "";    while(s.isEmpty()==false){        str = str+s.pop();    } return str;   }
}
0
harshscode2 weeks ago
  
stack<int> st;   for(int i=0;i<len;i++)   st.push(s[i]);    for(int i=0;i<len;i++)   {       s[i]=st.top();       st.pop();   }        return s;
0
himanshu0719cse193 weeks ago
Using Recursion Stack
public String reverse(String s){       if(s.length()==0)       {           return "";       }       char c=s.charAt(0);       String ans=reverse(s.substring(1));       ans=ans+c;       return ans;   }
0
amarrajsmart1971 month ago
 
C++  Easy Code .Time Taken =0.0/1.7 Sec.
stack<char> st;   for(int i=0;i<len;i++)   {       st.push(S[i]);   }   for(int i=0;i<len;i++)   {       S[i]=st.top();       st.pop();          }   return S;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.