Question: I need this code in C++. Write a recursive function bool is_palindrome(string str) that returns true if str is a palindrome, that is, a word
I need this code in C++.
Write a recursive function
bool is_palindrome(string str)
that returns true if str is a palindrome, that is, a word that is the same when reversed. Examples of palindrome are deed, rotor, or aibohphobia. Hint: A word is a palindrome if the first and last letters match and the remainder is also a palindrome.
-I have already created a function that does this, but my teacher is forcing me to use the given function bool is_palindrome(string str) and that is it. I have to do all of the computations within that one function, I cannot create more. She gave me a 0 for using char [] instead of string str, so I have to do it using the given function. The code from my previous attempt is here in case it helps:
#include
using namespace std; // A recursive function that check a str[s..e] // is palindrome or not. bool isPalRec(char str[], int s, int e) { // If there is only one character if (s == e) return true; // If first and last characters do not match if (str[s] != str[e]) return false; // If there are more than two characters, // check if middle substring is also // palindrome or not. if (s < e+1) return isPalRec(str, s+1, e-1); return true; } bool isPalindrome(char str[]) { int n = strlen(str); // An empty string is considered as // palindrome if (n == 0) return true; return isPalRec(str, 0, n-1); } // Main Function int main() { char str[100]; cout << "Enter a word to see if it's a Palidrome: "< cin >> str; if (isPalindrome(str)) printf("Yes, it's a palidrome."); else printf("No, it's not a Palidrome."); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
