In C++, write a stack-based application, that can be used to recognize palindrom
ID: 3671562 • Letter: I
Question
In C++, write a stack-based application, that can be used to recognize palindromes (strings spelled identically both backwards and forwards). The program must ignore spaces and punctuation, as well as alpha cases; so, for instance, the string “Madam I’m Adam” counts as a palindrome.
Supply push and pop operations. Use global variables for the stack and the stack pointer. Be sure that you program does not let the user push more than 25 characters
starting with...
char stack[25];
unsigned int sp = 0;
char pop();
void push(char);
...
int main()
It would also be appreciated if you can add comments explaining to whats happening in the code, thanks!
Explanation / Answer
#include<iostream>
#include<string>
#include<stack>
#include<queue>
using namespace std;
int main()
{
stack<char> s;
queue<char> q;
string str;
int i = 0;
char ch;
cout << "Enter a text in lowercase: ";
getline(cin, str);
cout << endl;
for (i=0; i<str.length(); i++)
{
ch = str[i];
q.push(ch); //compiler doesn't recognize enqueue so had to use push
s.push(ch);
}
while(!q.empty() && !s.empty())
{
if(q.front() != s.top())
{
cout << "The word " << str << " is not a palindrome" << endl;
break;
}
else if(q.front() = s.top())
{
i++;
q.pop(); //compiler doesn't recognize dequeue so had to use pop
s.pop();
if(i = str.length())
{
cout << "The word " << str << " is a palindrome" << endl;
break;
}
}
}
cin.get();
}