/
hacker_rank_day_18_palindrome_using_stack_and_queue_both.cpp
executable file
·77 lines (67 loc) · 1.62 KB
/
hacker_rank_day_18_palindrome_using_stack_and_queue_both.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
72
73
74
75
76
77
#include <iostream>
using namespace std;
class Solution {
//Write your code here
public:
int s=0,q=0,sl=0,ql=0;
string st,qe;
void pushCharacter(char a)
{
st[s]=a;
s++;
}
void enqueueCharacter(char a)
{
qe[q]=a;
q++;
}
char popCharacter()
{
sl=st.length();
char ch;
ch=st[sl-1];
st[sl-1]='\0';
return ch;
}
char dequeueCharacter()
{
ql=qe.length();
char ch;
ch=qe[0];
for(int i=0;i<ql-1;i++)
{
qe[i]=qe[i+1];
}
qe[ql-1]='\0';
return ch;
}
};
int main() {
// read the string s.
string s;
getline(cin, s);
// create the Solution class object p.
Solution obj;
// push/enqueue all the characters of string s to stack.
for (int i = 0; i < s.length(); i++) {
obj.pushCharacter(s[i]);
obj.enqueueCharacter(s[i]);
}
bool isPalindrome = true;
// pop the top character from stack.
// dequeue the first character from queue.
// compare both the characters.
for (int i = 0; i < s.length() / 2; i++) {
if (obj.popCharacter() != obj.dequeueCharacter()) {
isPalindrome = false;
break;
}
}
// finally print whether string s is palindrome or not.
if (isPalindrome) {
cout << "The word, " << s << ", is a palindrome.";
} else {
cout << "The word, " << s << ", is not a palindrome.";
}
return 0;
}