DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Roger has posted 34 posts at DZone. View Full User Profile

Palindrome Check In C

12.08.2008
| 10722 views |
  • submit to reddit
        // A palindrome is a string that reads the same both backwards and forwards. Famous examples
// are "Madam I'm Adam" or "Able was I ere I saw Elba". This function checks that a given string
// reads the same backwards and forwards. (requires including stdbool.h)

bool checkPalindrome(char *number) {
    char *begin, *end;
    int x;
    x = strlen(number) - 1;
    
    begin = &number[0];
    end = &number[x];
    
    for ( ; x >= 0; --x) {
        if ( *begin++ != *end--) return false;
    }
    
    return true;
}
    
    Tags:
  • C