#include #include #include void find_palindrome(const char *); int is_palindrome(const char *, int); const char *find_char(const char *, char); void find_palindrome(const char *text) { int i; int psize; const char *ith; const char *hit; for ( i = 0; text[i] != '\0'; i++ ) { if ( !isalnum(text[i]) ) { continue; } ith = &text[i]; hit = find_char( ith + 1, *ith ); while ( hit != NULL ) { psize = hit - ith + 1; if ( is_palindrome(ith, psize) ) { while ( ith < hit + 1 ) { putchar(*ith); ith++; } putchar('\n'); return; } hit = find_char( hit + 1, *ith ); } } } int is_palindrome(const char *chars, int size) { int l; int r; for ( l = 0, r = size - 1; l < r; l++, r-- ) { while ( !isalnum(chars[l]) ) { l++; } while ( !isalnum(chars[r]) ) { r--; } if ( tolower( chars[l] ) != tolower( chars[r] ) ) { return 0; } } return 1; } const char *find_char(const char *str, char ch){ int i; for ( i = 0; str[i] != '\0'; i++ ) { if ( tolower( ch ) == tolower( str[i] ) ) { return &str[i]; } } return NULL; } int main(int argc, const char **argv) { if ( argc <= 1 ) { fprintf(stderr, "USAGE:%s string\n", argv[0]); return 1; } find_palindrome(argv[1]); return 0; }