Coders Packet

Reverse of a String using stack in C++

By kamayani

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-

 

 

Download project

Reviews Report

Submitted by kamayani (kamayaniR)

Download packets of source code on Coders Packet