Recursion c++ A palindrome is a string that reads the same forward and backward.
ID: 3763841 • Letter: R
Question
Recursion c++
A palindrome is a string that reads the same forward and backward. Write a program that reads in any number of strings of characters from the user and determines if each string is a palindrome. The user will terminate each string by pressing the return (or enter) button. The user will indicate that there are no more strings by entering the string "quit". To do this you must write a recursive function named is Palindrome that takes a single MyString argument and two arguments that are bounds on array indices. The function must examine the part of the argument between the two bounds (including the bounds) and return true if this part of the argument is a palindrome, false if it is not a palindrome. The function must be recursive and must not call any other functions. In particular, you should not use the code you wrote in assignment 4.1. Follow this sample output closely: Enter a string: Able was I, ere I saw Elba Able was I, ere I saw Elba is a palindrome. Enter a string: peanut butter peanut butter is not a palindrome. Enter a string: quit In determining whether a string is a palindrome, this function must consider uppercase and lowercase letters to be the same and ignore punctuation characters and whitespace characters. You should not modify the argument in any way to accomplish this. The simplest way to handle uppercase/lowercase issues is to make everything uppercase on the fly, right at the instant that you are comparing the two characters.Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s){
int length = (int)s.length();
if (length == 0) {
return true;
}
return s[0] == s[length - 1] && isPalindrome(s.substr(1,length-2));
}
int main()
{
string word;
cout << "Please enter a word ";
getline(cin, word);
if (isPalindrome(word)) {
cout << "The string is a palindrome ";
}
else
cout << "The string is not a palindrome ";
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.