Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote