String Manipulation in C / C++ – 3 : check if palindrome

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.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: