-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseString.cpp
More file actions
31 lines (19 loc) · 925 Bytes
/
Copy pathreverseString.cpp
File metadata and controls
31 lines (19 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<stack> // including stack
using namespace std;
int main() {
string str = "umang";
stack <char> s; // stack ko s nam de diya
for (int i=0 ; i<str.length(); i++) { // pushing string umang into stack s ekk ekk kar kar push krte jaa rhe isme apn
char ch = str[i];
s.push(ch); // push character
}
string ans = ""; // ye ekkk bna liya apn n isme dalege reverse strink ko
// poping out from stack intp string ans as we know in stack there is last in first out so last inserted chr in string will be popped out
while(!s.empty()) { // kab tak chala rhe loop jab tak pura stack empty nhi hote
char ch = s.top(); // top wala stack me se liye apne isme g hoga ans.push_back(ch);
s.pop(); // pop
}
cout << "answer is: " << ans << endl; // ans me print hoga ye
return;
}