String Manipulation in C / C++ – 3 : check if palindrome
February 10, 2011 Leave a comment
Question 3. We wish to check if a given string is a palindrome.
One could use strcpy, strrev and strcmp from the string header, or one could proceed as follows:
// Returns 1 if string is palindrome, else returns 0.
bool isPalindrome ( const char *s)
for (int i =0, j = strlen (s) – 1 ; (i < j) && ( *(s+i) == *(s+j) ); i++, j–);
return ! ( i < j );
Once again, it is good practice to check for the function working properly for null strings, even-length and odd-length strings.