In: Computer Science
word level palindrome program with stacks and queues
I have a good portion of my program finished, I am just trying to figure out how to do some final things to it. I want the program to only check for letters a through z and A = Z, treating uppercase and lower case as the same thing. I want it to ignore any other characters and to just realize word level palindromes, not sentence level, so for example a%345b@a c24&c8d)9cc would be a word level palindrome since the program would ignore the numbers and special characters and only look at each word or set of letters as a candidate. Since uppercase and lowercase will be treated the same, something like AbBa baB should also come back as a word level palindrome. Here is the code I have so far. Thanks for any help.
include <cassert> // Provides assert
include <cctype> // Provides isalpha, toupper
include <iostream> // Provides cout, cin, peek
include <queue> // Provides the queue template class
include <stack> // Provides the stack template class
using namespace std;
int main() {
queue<char> q; stack<char> s; char letter; queue<char>::size_type mismatches = 0; // Mismatches between queue and stack cout << "Enter a line and I will see if it's a palindrome:" << endl; while (cin.peek() != '\n') { cin >> letter; if (isalpha(letter)) { q.push(toupper(letter)); s.push(toupper(letter)); } } while ((!q.empty()) && (!s.empty())) { if (q.front() != s.top()) ++mismatches; q.pop(); s.pop(); } if (mismatches == 0) cout << "That is a palindrome." << endl; else cout << "That is not a palindrome." << endl; cin.ignore(2); return 0;
}
Answer:
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main (void)
{
queue <char> value;
string input;
int length;
cout<<"Please enter a series of
characters."<<endl;
getline (cin, input);
bool isPalindrome = false;
if (inputs.size() > 0)
{
int length = input.size() / 2;
for (int i=0; i<length; i++)
{
value.push(inputs[i]);
}
isPalindrome = true;
for (int i = 1; i <= length &&
isPalindrome; ++i)
{
isPalindrome = value.front() ==
inputs[inputs.size() - i];
value.pop();
}
}
if(!isPalindrome)
{
cout<<"Is not a
palindrome."<<endl;
}
else
{
cout<<"Is a
palindrome."<<endl;
}
return 0;
}