Question

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 ask the user to type a text, and then print out the text is palindrome or not, until the user wants to quit the program. (Note: you might delete any punctuation signs firstly, only need to consider the letters)

(C++ Programming)

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

#include <iostream>

#include <cctype>

using namespace std;

//function declarations

bool testPalindrome(string str);

string upperCaseIt(string s);

int main()

{

//Declaring variables

string str,str1;

char ch;

  

/* This while loop continues to execute

* until the user enters other than 'y' or 'Y'

*/

while(true)

{

str1="";

//Getting the input entered by the user

cout<<"\nEnter a string :";

getline(cin,str);

  

//eliminating spaces,commas..etc

for(int i=0;i<str.length();i++)

{

if(isalpha(str[i]))

str1+=str[i];

}

//calling the function

str1=upperCaseIt(str1);

//calling the function

if(testPalindrome(str1))

cout<<"This is a Palindrome"<<endl;

else

cout<<"This is not a Palindrome"<<endl;  

cout<<"\nDo you Want to continue(Y/N):";

cin>>ch;

if(ch=='y'||ch=='Y')

{

cin.ignore();

continue;

}else

{

break;

}

}

return 0;

}

/* This is a recursive function which calculates

* whether the String is Palindrome or not using recursive method

* Param:string

* return: boolean

*/

bool testPalindrome(string str) {

if(str.length() == 0 || str.length() == 1)

return true;

else if(str[0] == str[str.length()-1])

return testPalindrome(str.substr(1, str.length()-2));

  

return false;

}

// This function will convert the lower case to upper case

string upperCaseIt(string s)

{

for (int i = 0; i < s.size(); i++)

{

if (s.at(i) >= 97 && s.at(i) <= 122)

{

s.at(i) = s.at(i) - 32;

}

}

return s;

}

_____________________

Output:

ב c:\ CAProgram Files (x86) Dev-CpplMinGW641bin\PlaindromelrrespectiveOfSpacesCommaR Files Enter a string Able was I. ere I s

______________Thank You

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

  • Using C language Palindromes A palindrome is a word or phrase that reads the same backwards...

    Using C language Palindromes A palindrome is a word or phrase that reads the same backwards and forwards, ignoring punctuation, spaces, and anything that isn’t a letter. Examples of Palindromes Madam, I’m Adam. Kayak Racecar A man, a plan, a canal – Panama! Able was I, ere I saw Elba. Go hang a salami, I’m a lasagna hog! Write a program that does the following: opens a text file named “Proj3input.txt” containing multiple lines of text (1 or more) 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...

  • 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

  • 1 of 1 15 points) Palindrome Detector. A palindrome is a phrase that reads the Pi...

    1 of 1 15 points) Palindrome Detector. A palindrome is a phrase that reads the Pi same ronwards as it does backwards. For example, "a man, a plan, a canal, Panama is a palindrome. Write a program that uses a stack to check for palindromes in each line of a text file. Try your program on the example text file "palindromes txt".Your program should output the palindromes that it finds in the document. For example: FindPalindromes( palindromes.txt") will display "a...

  • Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward....

    Problem 1: (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 testPa1indrome 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. Please I need it in C program and...

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

  • A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks...

    A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks 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 that will accept a sequence of characters terminated by a period and will decide whether the string—without the period—is a palindrome. You may assume that the...

  • A palindrome is a word or phrase that reads the same both forwards and backwards. "racecar"...

    A palindrome is a word or phrase that reads the same both forwards and backwards. "racecar" is a palindrome. So is, "a man a plan a canal, panama" if you ignore the spacing and the comma. Discovering how to automate the detection of palindromes is a good way to test your understanding of loops and strings. The file, "hw2problem5.py", contains a function called is_palindrome(), which has a single string parameter, s. If s is a palindrome, then is_palindrome() returns the...

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