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

Write a recursive function named isPalindrome that accepts a string parameter an

ID: 3838747 • Letter: W

Question

Write a recursive function named isPalindrome that accepts a string parameter and returns true if the string is the same forwards as backwards, ignoring capitalization. For example, the call of isPalindrome("Madam") should return true.

I only get 9/13 correct and am unsure as to why.

write a recursive function named isPalindrome that accepts a string parameter and returns true if the string is the same forwards as backwards, ignoring capitalization. For example, the call of isPalindrome ("Madam") should return true. Constraints: Do not declare any global variables or any auxiliary data structures. Do not use any loops; you must use recursion. Type your solution here: 1 bool is Palindrome (string str) 3 if (str. length() str length 1) return true return isPalindrome (str.substr C1, str length 1)) 8 else if (str.at (0) str.at (1)) return true; 10 else if (str.at (0) E str.at (str. length() 1)) return true 12 return isPalindrome (str.substr C1, str length 1)) 13 14 15 16 17 else return false; 18 19

Explanation / Answer

#include <string>

#include <cctype>

#include <iostream>

#include <cstdlib>

#include <cassert>

bool isPalindrome(const std::string& str, int pos = 0){

  

if(((str.end() - pos ) - (str.begin() + pos)) <= 1) return true;

  

if(std::tolower(*(str.begin() + pos)) == std::tolower(*(str.end()- pos -1))){

return isPalindrome(str, pos + 1);

}

else{

return false;

}

}

int main(){

assert(isPalindrome("madam") == true);

assert(isPalindrome("racecar") == true);

assert(isPalindrome("Step ON No pEts") == true);

assert(isPalindrome("able was I ere I saw Elba") == true);

assert(isPalindrome("BB") == true);

assert(isPalindrome("Q") == true);

assert(isPalindrome("") == true);

assert(isPalindrome("Java") == false);

assert(isPalindrome("rotater") == false);

assert(isPalindrome("byebye") == false);

assert(isPalindrome("notion") == false);

assert(isPalindrome("xerox") == false);

assert(isPalindrome("XY") == false);

}

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