-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringreverse.cpp
71 lines (63 loc) · 1.07 KB
/
stringreverse.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>
#include <string>
#define SIZE 20
std::string input;
std::string reversed;
template <class T>
class stack
{
int top;
T* array;
int n;
public:
stack()
{
n = SIZE;
top = -1;
array = new T[n];
}
void push(T data)
{
if(top == n - 1)
{
std::cout<<"Stack is full!!"<<std::endl;
exit(1);
}
array[++top] = data;
}
T pop()
{
if(top == -1)
{
std::cout<<"Stack is empty!!"<<std::endl;
exit(1);
}
return array[top--];
}
bool empty()
{
if(top == -1) return true;
return false;
}
};
void ReverseString(stack<char> &s)
{
for(int i = 0;i<input.size();i++)
{
s.push(input[i]);
}
for(int i = 0;i<input.size();i++)
{
reversed += s.pop();
}
}
int main()
{
std::cout << "Enter The String to be Reversed: ";
std::cin >> input;
stack<char> s;
ReverseString(s);
std::cout <<"The Reversed String is: "<< reversed << std::endl;
return 0;
}