Complete the code for -- isPalindromeHelper(const char *s1, int len); bool isPal
ID: 3730082 • Letter: C
Question
Complete the code for --
isPalindromeHelper(const char *s1, int len);
bool isPalindrome(const char *s1);
#include #include #include #include "include "strFuncs.h" using namespace std; bool isPalindromeHelper (const char *s1, int len); /Precondition: s1 is a valid C-string that may contain upper or lower case alphabets, no spaces or special characters //Postcondition: Returns true if s1 is a palindrome, false otherwise //You MUST provide a recursive implementation and are recommended to write a helper function where the recursion actually takes place* bool isPalindrome(const char s1)Explanation / Answer
bool isPalindrome(char *str)
{
int n = strlen(str);
// An empty string is considered as
// palindrome
if (n == 0)
return true;
return isPalindromeHelper(str, n);
}
bool isPalindromeHelper(const char *s1, int len){
// If there is only one character
if (len <= 1)
return true;
// If first and last characters do not match
if (*s1 != *(s1+len-1))
return false;
// If there are more than two characters,
// check if middle substring is also
// palindrome or not.
return isPalindromeHelper(++s1, len-1);
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.