I was learning about recursion and was attempting a palindrome check. It is not working. Can someone help me figure out the reason.
private static bool CheckPalin(string p)
{
if(p.Length == 1 || p.Length == 0) //added check for even cases
return true;
if(p[0] != p[p.Length -1])
return false;
CheckPalin(p.SubsString(1, p.Length -2));
return true;
}
Dan hit one failure square on the head (as implied by my question about
aa— length0strings will give you trouble).The other failure case, as indicated by false positives is actually pretty obvious once you take a quick step back and look for it:
Try:
It’s amazing, I skimmed it twice without noticing it — until you pointed out the false positives. 🙂