Question

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 the user input. The user can type a single line of input and hit enter. The input can contain any characters, including uppercase and lowercase letters, digits, special symbols, spaces, etc. The program will check the input against the palindrome properties, will display the result, and ask for an input again. The user can check multiple lines before deciding to exit the program. The user can type quit/QUIT/Quit or any combination of uppercase/lowercase letters in quit and exit the program immediately.

Output:

There are two possible outputs from the program:

If the input string is a palindrome, then the program will display "The input is a palindrome."

If the input string is not a palindrome, then the program will display "The input is not a palindrome."

Programming instructions:

Follow these instructions as closely as possible. They will help you in coming up with the right solution. Deviations from requirements (indicated with asterisks) will result in penalties.

  1. Declare the input string to be 100 characters maximum. Some of the input strings are quite long.
  2. Make sure to compare the user input against the word "QUIT" before taking additional steps. The comparison should be case insensitive. Use function strcasecmp() or something similar for case insensitive comparison (you'll need to include string.h header file on Linux/Unix).
  3. (*) Your program should declare and make use of exactly one stack and one queue. The stack and the queue should store single characters as elements. Do NOT use <stack> and <queue> headers
  4. (*) Read the input string and load alpha characters into the stack and into the queue at the same time (ignore all the other characters that are not letters). Convert the letters to lowercase or to uppercase before loading onto the stack or into the queue. This will simplify the further comparison.
  5. (*) Your program should contain one boolean function, called isPalindrome, which accepts one stack and one queue as an input and determines whether their contents are matching or not.
  6. After each iteration and at the beginning of each iteration, the stack and the queue should be completely empty.
  7. (*) You should allow the user to enter strings containing spaces. I recommend that you use cin.getline function to read the input.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

/* C++ Program that reads in a string and determines whether it is palindrome or not */

#include <iostream>

using namespace std;

//Function prototype
bool isAPalindrome(char[], int, int);

//Main function
int main()
{
char str[256];
int length;

//Reading a string
cout << "\n Please enter a string to test for palindrome or type QUIT to exit: ";
cin.getline(str, 256);

//Loop till user want's to quit
while(strcmp(str, "QUIT") != 0)
{
//Find out the length
length = strlen(str);

//Call the function and check whether given string is palindrome or not
if(isAPalindrome(str, 0, length-1))
cout << "\n " << str << " is a palindrome.";
else
cout << "\n " << str << " is not a palindrome.";

//Reading string again
cout << "\n\n Please enter a string to test for palindrome or type QUIT to exit: ";
cin.getline(str, 256);
}

cout << endl;
return 0;
}

//Function that checks whether given string is palindrome or not
bool isAPalindrome(char str[], int lowerBound, int upperBound)
{
char ch1,ch2;

//Checking for boundary crossings
if(lowerBound > upperBound)
return true;

//Escaping white spaces and punctuation marks
while(lowerBound <= upperBound)
{
//Checking for punctuation and spaces
if(ispunct(str[lowerBound]) || isspace(str[lowerBound]))
lowerBound ++;
else
break;
}

//Escaping white spaces and punctuation marks
while(upperBound >= lowerBound)
{
//Checking for punctuation and spaces
if(ispunct(str[upperBound]) || isspace(str[upperBound]))
upperBound --;
else
break;
}

//Getting two characters in upper case
ch1 = str[lowerBound];
ch1 = toupper(ch1);

ch2 = str[upperBound];
ch2 = toupper(ch2);

//Comparing characters
if(ch1 != ch2)
return false;

//Incrementing lower bound
lowerBound++;

//Decrementing upper bound
upperBound--;

//Recursive call
return isAPalindrome(str, lowerBound, upperBound);
}
_______________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...
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
  • 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....

  • 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...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • 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...

  • 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...

  • A Palindrome is a string that is spelled the same way forward and backward (example: radar)....

    A Palindrome is a string that is spelled the same way forward and backward (example: radar). Write a Java program that asks the user to input a string and tests whether the string is a Palindrome or not. Display the message: "The string is a Palindrome" if it is, or "The string is NOT a Palindrome" if it is not. Assume that the user will enter a string without any spaces. The string can be any length. The String can...

  • Palindrome Detector A palindrome is any word, phase, or sentences that reads the same forward and...

    Palindrome Detector A palindrome is any word, phase, or sentences that reads the same forward and backward. Here are some well-know palindrome. Write a bool function that uses recursion to determine if a string arguments is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program

  • C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and...

    C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are: "radar" "able was i ere i saw elba" and, if you ignore blanks: "a man a plan a canal panama" Write a recursive function testPalindrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Use the function in a complete program...

  • Write a C program that prompts the user for an input string with all lowercase letters....

    Write a C program that prompts the user for an input string with all lowercase letters. Your program should then sort the characters in the string and print the sorted string. Assume that the string contains no spaces and has at most 30 lowercase characters. Your program should loop continually until the user enters “q” to quit.

  • #19 with the following instructions 19. A palindrome is a string that can be read backward...

    #19 with the following instructions 19. A palindrome is a string that can be read backward and forward with the same result. For example, the following is a palindrome: Able was I ere I saw Elba. unction to test if a string is a palindrome using a stack. You can push characters in the stack one by one. When you reach the en string, you can pop the characters and form a new string. If the two strings are exactly...

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