Showing posts with label reverse string. Show all posts
Showing posts with label reverse string. Show all posts

Sunday, November 22, 2020

Reverse String | interviewBit | Solution | C++

Problem : 



Given a string S, reverse the string using stack.
Example :
Input : "abc" 
Return "cba"



Solution1 : 

string Solution::reverseString(string A) {
    reverse(A.begin(),A.end());
    return A;
    
}


Solution2 :

string Solution::reverseString(string A) {
    stack<char>s;
    int l = A.length();
    for(int i =0;i<l;i++){
        s.push(A[i]);
    }
    
    string rev = "";
    
    while(!s.empty()){
        rev+=s.top();
        s.pop();
    }
    
    return rev;
   
    
}