C++, The palindrome sample code we discussed during class determines whether a s
ID: 3814254 • Letter: C
Question
C++, The palindrome sample code we discussed during class determines whether a string reads the same forward and backward. For example, the string “Able was I ere I saw Elba” is considered a palindrome if we treat both the upper- and lowercase versions of a letter as being the same character. This version of definition of palindrome is actually a “string-level” palindrome.
In this programming exercise, you are required to realize “word-level” palindrome checker program, which means if each word in the input string reads the same forward and backward, then the input string is considered as a “word-level” palindrome. For example, “aba abba” is not a “string-level” palindrome, but it is a “word-level” palindrome.
The program only checks letters (‘a’ to ‘z’ and ‘A’ to ‘Z’), but not any digit or other symbols. For example, the input string “a1112234ba #$%^& c2c4d5dcc” is a word-level palindrome.
Treat both the upper- and lowercase versions of a letter as being the same character. In this way “Aa bB” is a “word-level” palindrome.
Identify four test cases other than the examples given above. At least one test case is a “word-level” palindrome, and at least one test case is not a “word-level” palindrome. At least one test case should contain symbols other than letters and white spaces. For each of the four test cases, show what input you will use and what your expected outputs would be.
//What I have so far, but I keep getting "This is not a Palindrone output".
#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() != ' ')
{
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;
}
The output should look like these seperate examples:
Enter a line and I will see if it's a word level Palindrome:
Able was I ere I saw Elba
That is not a word-level palindrone.
Enter a line and I will see if it's a word level palindrome:
aba abba
That is a word-level palindrone.
Enter a line and I will see if it's a word level palindrome:
a1112234ba #$%^& c2c4d5dcc
That is a word-level palindrone.
Enter a line and I will see if it's a word level palindrome:
Aa bB
That is a word-level palindrone.
Explanation / Answer
Fixed your code. Basically error you were making is you were inserting whole string in queue and stack and then checking for mismatch which was causing normal palindrome check and not word palindrome check. You need to check as soon as you encounter space.
#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
#include <string>
using namespace std;
int isPalindrome(queue<char> q, stack<char> s)
{
while ((!q.empty()) && (!s.empty()))
{
if (q.front() != s.top())
return 0;
q.pop();
s.pop();
}
return 1;
}
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;
int isWordPalindome = 1;
string line;
getline(cin, line);
for(int i = 0; i < line.length(); i++)
{
char letter = line[i];
if (isalpha(letter))
{
q.push(toupper(letter));
s.push(toupper(letter));
}
if(isspace(letter) && !q.empty())
{
if(!isPalindrome(q, s))
{
isWordPalindome = 0;
break;
}
queue<char> empty;
swap( q, empty );
stack<char> emptyStack;
swap(s, emptyStack);
}
}
if (isWordPalindome)
cout << "That is a word-palindrome." << endl;
else
cout << "That is not a word-palindrome." << endl;
cin.ignore(2);
return 0;
}
If this has helped you please provide a positive feedback.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.