The more I think about it, the more I think that zero-length strings ARE palindromes. This is especially important in recursive algorithms applied to even-length strings.
Could you please elaborate?
OK. One algorithm I've seen is:
If the string length is 0, return TRUE *
If the string length is 1, return TRUE
If the first character is different than the last character, return FALSE
Otherwise, recursively check the string minus the first and last character
The *'d line is what I'm referring to. If the TRUE is changed to a FALSE, then every even-length palindrome will incorrectly return FALSE.
See my solution, just treat 2-length strings differently, so that it only checks if both characters are the same, and then return true or false in stead of invoking itself with string length
Well, reversing the string is off course a very simple and wise choice not to over-complicate the code and move on to the next task. If speed would be a goal (when using it very often), it would depend on the type of input data. If you would check the English
dictionary for Palindromes, the reversing strings step could in the end be a more time consuming task than moving pointers, compare and return false after the first few iterations in most cases...