// hw12.cpp
// Function implemented by
// Crystal Torres
// Section 1MA3C
/**
* This is a driver program that reads a phrase from the user, and calls
* the function that you are supposed to write for homework 12 to test
* whether it is a palindrome.
*
* Author: JDD
*/
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;
// This function is the one you should write. It must use a stack and queue
// to determine whether the c-string is a palindrome or not. Spaces and
// other puctuation marks should be ignored. The palindrome should not be
// case sensitive.
bool isPalindrome(const char* phrase);
int main() {
string phrase;
cout << "Enter a phrase:>";
getline(cin, phrase);
if (isPalindrome(phrase.c_str()))
cout << phrase << " is a palindrome." << endl;
else
cout << phrase << " is not a palindrome." << endl;
return 0;
}
bool isPalindrome (const char* phrase) {
int size = strlen(phrase);
stack<char> backward;
queue<char> forward;
for (int i = 0; i < size; i++) {
if (isalpha(phrase[i]))
backward.push(toupper(phrase[i]));
}
for (int i = 0; i < size; i++) {
if (isalpha(phrase[i]))
forward.push(toupper(phrase[i]));
}
while ( ! backward.empty()) {
if (backward.top() == forward.front()) {
backward.pop();
forward.pop();
}
else
return false;
}
return true;
}