1) Write a program that reads a string of characters, pushing each character ont
ID: 3700092 • Letter: 1
Question
1) 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.
2) Modify the basic assignment so it queries the user for the name of a file containing one word per line. Open that file, process each word using basic stack and queue operations to determine if it is a palindrome, and if so, print it. Do NOT treat one character strings as a palindrome (so the word a is not a palindrome). At the end report the total number of palindromes found and the total number of words processed. Test your program on the dictionary file found on fang:
/usr/share/dict/web2 That file contains 235924 words so that is what your program should report. For the purpose of this assignment ignore case, so Paap would be a palindrome.
need this to be written in c++ with complete documentation
Explanation / Answer
#include <iostream>
#include <stack>
#include <queue>
#include <string>
using namespace std;
bool operator==(const stack<char>&, const queue<char>&);
bool operator!=(const stack<char>&, const queue<char>&);
int main(int argc, char *argv[]) {
stack<char> *s;
queue<char> *q;
string input;
string::iterator i;
while (true) {
s = new stack<char>;
q = new queue<char>;
cout << "> ";
getline(cin,input);
for (i = input.begin(); i != input.end(); i++) {
s->push(*i);
q->push(*i);
}
cout << input << " is ";
if (*s != *q) {
cout << "not ";
}
cout << "a palindrome." << endl;
delete s;
delete q;
}
return 0;
}
bool operator==(const stack<char> &s1, const queue<char> &q1) {
bool eq = true;
stack<char> s = s1;
queue<char> q = q1;
if (s.size() == q.size()) {
while ((s.empty() == false) && (eq == true)) {
eq = (s.top() == q.front());
s.pop();
q.pop();
}
} else {
eq = false;
}
return eq;
}
bool operator!=(const stack<char> &s, const queue<char> &q) {
return !(s == q);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.