Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are given a function that takes a constant C string as input. In this functi

ID: 3730004 • Letter: Y

Question

You are given a function that takes a constant C string as input. In this function you MUST use a recursive implementation and you may not use built-in C++ functions that we have NOT discussed in lecture. It is highly recommended that you implement a helper function with the right set of parameters for implementing the recursive part.

/* 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 bool isPalindrome(const char *s1) return true;

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote