-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.cpp
More file actions
86 lines (68 loc) · 1.19 KB
/
Palindrome.cpp
File metadata and controls
86 lines (68 loc) · 1.19 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
struct Stack
{
int size;
int top;
char *s;
};
bool isFull(struct Stack stk)
{
return stk.size == stk.top-1;
}
bool isEmpty(struct Stack stk)
{
return stk.top == -1;
}
void push(struct Stack *stk, char ch)
{
if(isFull(*stk))
{
cout << "Stack is Full\n";
return;
}
stk->top++;
stk->s[stk->top] = ch;
}
char pop(struct Stack *stk)
{
if(isEmpty(*stk))
{
cout << "Stack is Empty\n";
return -1;
}
char c = stk->s[stk->top];
stk->top--;
return c;
}
bool isPalindrome(string str1, string str2)
{
return str1 == str2;
}
int main()
{
string chars;
cin >> chars;
struct Stack stk;
stk.size = chars.size();
stk.top = -1;
stk.s = new char[stk.size];
for(int i=0; i<chars.size(); i++)
{
push(&stk,chars[i]);
}
string BackwardChar;
while(!isEmpty(stk))
{
BackwardChar.push_back(pop(&stk));
}
switch (isPalindrome(chars,BackwardChar))
{
case true:
cout << "Is Palindrome\n";
break;
case false:
cout << "Is not a Palindrome\n";
break;
}
}