Reverse of a String using stack in C++

kamayani Jul 07, 2021

In this C++code, we can easily reverse a string using the stacks. once the input is given, it gives the output string in the reverse format.

STEPS-
1) Create an empty stack.
2) One by one push all characters of string to stack.
3) One by one pop all characters from stack and put them back to string.

CODE-

#include 
#include 
#include 
#include 


using namespace std;

int main()
{
    int i=0;
    string text;
    stack s;
    cout<<"Enter a string you want reverse of: ";
    cin>>text;
    for(int i=0;i<text.length();i++)
    {
        s.push(text[i]);
    }
    while(!s.empty())
    {
        cout<<s.top();
        s.pop();
    }
}

 

OUTPUT-

 

 

Project Files

Loading...
..
This directory is empty.

Comments (0)

Leave a Comment