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

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

  • A palindrome is a word, phrase, number, or other sequence of characters which reads the same...

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Write a recursive method in java that accepts a integer as its argument and returns true if it is palindrome, false otherwise. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“Is 12321 a palindrome? “+ra.isPalindrome(12321)); //true                 }                 public Boolean isPalindrome(int num){                                 return false;...

  • please explain the answer A sequence of characters is called a palindrome if it reads the...

    please explain the answer A sequence of characters is called a palindrome if it reads the same way forward or backward. For example, 59AA95 is a six-character palindrome, and 59A95 is a five-character palindrome. Some other instances of palindromes: U NU, LON NOL, MALAYALAM, NOW ON, PUT UP, TOO HOT TO HOOT, NEVER ODD OR EVEN, ABLE WAS I ERE I SAW ELBA, and POOR DAN IS IN A DROOP. Find the number of nine-character palindromes that can be formed...

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