Question

Palindrome is a word or a phrase when taken in reverse order, gives the same word...

Palindrome is a word or a phrase when taken in reverse order, gives the same word or phrase. For example, the word “radar” is a palindrome. The phrase "Do geese see God?" is also a palindrome after the removal of the question mark and all the spaces and the conversion of the remaining alphabets to either upper or lower case.

Write a program that uses an STL stack to check if an arbitrary string containing multiple words separated by spaces (do not use a single word to test your program!) is a palindrome. You may use either a C-string and/or a C++ string to store the word or phrase to be tested. Besides the main() function, the program must include the following additional functions:

  • inputString, which prompts the user to enter a phrase (instead of just a word) and store it in a string object
  • convert2lower, which a) removes punctuation and blank characters in the string and b) converts all alphabets in the string to lower case
  • isPalindrone, which checks the converted input string to determine if it is a palindrome. It returns true or false depending on whether the string is or is not a palindrome.

The following websites provide useful information related to the above problem:

http://www.tutorialspoint.com/ansi_c/c_function_references.htm

http://www.cplusplus.com/reference/string/string/

http://www.cplusplus.com/reference/stack/stack/

https://en.wikipedia.org/wiki/Palindrome

Note: The file you turned in must contain a) properly documented source code plus b) screen capture of the program output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include<iostream>
#include<string.h>
#include <bits/stdc++.h>

using namespace std;

// function to prompts the user to enter a phrase
//and store it in a string object
void inputString(string &phrase)
{
//prompts the user to enter a phrase
cout << "Enter a phrase: ";
//read the phrase
getline(cin, phrase);
}

//function to removes punctuation and blank characters in the
//string and converts all alphabets in the string to lower case
string convert2lower(string phrase)
{
string str = "";
for(int i=0; i<phrase.size(); i++)
{
//skipping space and punctuation
if(ispunct(phrase[i]) || isspace(phrase[i]))
continue;
//convert to lower case
str = str + (char)(tolower(phrase[i]));
}
return str;
}

//function to return whether the phrase is a Palindrome
bool isPalindrone(string phrase)
{
stack <char> s;
int i;
int mid = phrase.size()/2;
for(i=0; i<mid; i++){
s.push(phrase[i]);
}

if(phrase.size()%2!=0)
i++;

for(;i<phrase.size(); i++)
{
char ch = s.top();
if(ch!=phrase[i])
return false;
s.pop();
}

return true;
}

//main function
int main()
{
string phrase, str;
bool fg;

//read the phrase
inputString(phrase);

//removes punctuation, blank characters and converts to lower case
str = convert2lower(phrase);

//check whether the phrase is a Palindrome
fg = isPalindrone(str);

if(fg)
cout<<"\""<<phrase<<"\" "<< "is a Palindrome"<<endl;
else
cout<<"\""<<phrase<<"\" "<< "is not a Palindrome"<<endl;

return 0;
}


Output:

Enter a phrase: able was i ere i saw elba
"able was i ere i saw elba" is a Palindrome

Enter a phrase: a man a plan a canal panama
"a man a plan a canal panama" is a Palindrome

Enter a phrase: Do geese see God
"Do geese see God" is a Palindrome

Enter a phrase: good bye!
"good bye!" is not a Palindrome

N.B.  Whether you face any problem then share with me in the comment section, I'll happy to help you.

Add a comment
Know the answer?
Add Answer to:
Palindrome is a word or a phrase when taken in reverse order, gives the same word...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • This is a java question A palindrome is a word or phrase that reads the same...

    This is a java question A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: warts n straw radar Able was I ere I saw Elba tacocat Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is...

  • C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same...

    C++ Programming Palindrome Detector: A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man,a plan, a canal, Panama Desserts, I stressed Kayak Write a book function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program, which continues to...

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT