Sunday, 13 December 2015

Reverse a string using Stack API in C++

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;

void reverse(char *p);
int main()
{
    char str[] = "Hello Bro !!";
    reverse(str);
    cout<<str;
    return 0;
}

void reverse(char *p){
    stack<char> s;
    for(int i=0;i<strlen(p);i++){
        s.push(p[i]);
    }
    for(int i=0;i<strlen(p);i++){
        p[i]=s.top();
        s.pop();
    }
    cout<<endl;
}

No comments:

Post a Comment