Question

In: Computer Science

word level palindrome program with stacks and queues I have a good portion of my program...

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;

}

Solutions

Expert Solution

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


Related Solutions

how do I copy my word data to excel I typed my report on word document...
how do I copy my word data to excel I typed my report on word document and would like to copy them on excel 2013 in other to add table and graph
I wrote this program to check a string is palindrome or not. but in both cases,...
I wrote this program to check a string is palindrome or not. but in both cases, it gives me palindrome. could someone help me with this program? it has an error I couldn't find. when I run the code and give a string, in both cases it gives me palindrome. for example if I give Pop it says it is a palindrome but if I give Salima, it also says palindrome. #include<string> #include <iostream> using namespace std; class PString:public string...
c++: I need #include <iostream> header A palindrome (Links to an external site.) is a word,...
c++: I need #include <iostream> header A palindrome (Links to an external site.) is a word, phrase, number, or sequence of words that reads the same backward as forward. Punctuation, capitalization, and spaces between the words or lettering are allowed. Here are some examples of word and phrase palindromes. Words: Civic Kayak Level Madam Mom Noon Racecar Radar Refer Rotor Phrases: Don't nod. I did, did I? My gym Step on no pets Top spot Never odd or even Eva,...
I have a ROI group project for my masters class. There's a portion Idon't really know...
I have a ROI group project for my masters class. There's a portion Idon't really know how to approach. This portion would be a: " Figures of Merit    -    Report Analysis This section reviews the Net Present Value, Benefit Cost Ratio, and Internal Rate of Return and why these measures support your project." How would I write this report?
Below is my C++ program of a student record system. I have done the program through...
Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks. #include<iostream> #include<string> using namespace std; struct StudentRecord {    string name;    int roll_no;    string department;    StudentRecord *next; }; StudentRecord *head = NULL; StudentRecord *get_data() {...
how can i lower my expences when i know my income is vere good but my...
how can i lower my expences when i know my income is vere good but my expences always ALMOST reaches my income but i know i cant do anything as i know i need this liabilities. what can i do or what strategy can i make in order to lower my expences!!! ?????
My Teacher keeps telling me I don't have a Thesis. I thought I hada good thesis....
My Teacher keeps telling me I don't have a Thesis. I thought I hada good thesis. Please help! Bud Light in advertising             One of the most effective ways for businesses to thrive is to use effective advertising, whether the ad is on TV, magazines, social media or even movies. The goal is to create enticement for a purchase over the competitor’s product. Budweiser is great example of a billion dollar company that has always maintained a strong record of...
I have a program to code for my computer science class, and Im not sure how...
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it. Problem Description: Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Define the Rectangle2D class that extends...
I need a good introduction to my paper on Freuds Psychoanylic theory and a good thesis...
I need a good introduction to my paper on Freuds Psychoanylic theory and a good thesis statement
It is very common, for program managers to have a portion of their bonus linked to...
It is very common, for program managers to have a portion of their bonus linked to program sales and specifically, how monthly sales match up with target monthly sales goals. Your supervisor, the program manager has explained to you the bonus calculation and has asked for your help. This months sales were amazing and your supervisor has asked you on the last day of the month to hold off entering your last 2 sales until the next day to "push"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT