Write a program that reads a string of characters, pushing each character onto a
ID: 3707583 • Letter: W
Question
Write a program that reads a string of characters, pushing each character onto a stack as it is read and simultaneously adding it to a queue. When the end of the string is encountered the program should use basic stack and queue operations to determine if the string is a palindrome. Your solution must use the STL stack and queue classes.
Please do not use any programs that answered this prompt before. I would like to see a different way of the program in order better understand the different methods of writing a program.
Explanation / Answer
CODING:
#include <iostream>
#include <stack>
#include <queue>
#include <string>
using namespace std;
int main()
{
stack<char> mys;
queue<char> myq;
string s;
cout<<"Enter a string:";
cin>> s;
int l = s.length();
int hat=0;
for(int i=0;i<l;i++)
{
mys.push(s.at(i));
myq.push(s.at(i));
}
for(int i=0;i<l;i++)
{
if(myq.front()!=mys.top())
{
hat=1;
break;
}
myq.pop();
mys.pop();
}
if(hat==0)
{
cout<<"Palindrome"<<endl;
}
else
{
cout<<"Not a Palindrome"<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.