-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverse_string.cpp
More file actions
40 lines (35 loc) · 833 Bytes
/
reverse_string.cpp
File metadata and controls
40 lines (35 loc) · 833 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
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<stack>
#include<string>
using namespace std;
stack<char> s;
void reverse(string str){
/*
Objective: This function prints reverse of the string.
Input parameter: None
Functions used: empty() -> returns True if stack is empty, otherwise False.
top() -> returns top element of the stack.
pop() -> removes the top element from the stack
Return value: None
Side effects: The stack has been emptied.
*/
for(int i=0;i<str.length();i++)
s.push(str[i]);
cout<<"\nReversed string: ";
while(!s.empty()){
cout<<s.top();
s.pop();
}
cout<<"\n";
}
int main(){
/*
objective: To reverse given set of numbers using function input() and reverse()
*/
cout<<"\n\t\tPROGRAM TO REVERSE A STRING";
string str;
cout<<"\n\nEnter string: ";
cin>>str;
reverse(str);
return 0;
}