Surviving the Technical Interview

Dr. Dobb's Journal Fall 1998

By Jay Sutaria

int IsPalindrome (char * myStr) {
    /* we'll return -1 if this is not a palindrome */
    /* else we'll return 1 */
    int ptr1, ptr2;
    /* point to first character in the string */
    ptr1 = myStr;
    /* point to last character in the string */
    ptr2 = myStr + strlen(myStr) - 1 ;
    while (ptr1 < ptr2) {
        /* if we find characters that don't match,
           then this isn't a palindrome */
        if (*ptr1 != *ptr2) {
            return (-1);
        }
        ptr1++;
        ptr2--;
    }
    return (1);
}

Example 5: Is a string a palindrome?

Back to Article


Copyright © 1998, Dr. Dobb's Journal