"how to remove other characters other than letters?"
I think you don't want to actually remove it, just skip it and you could use isalpha
to do so. Also note that condition ptr2 > ptr1
will work only for strings with even amount of characters such as abba
, but for strings such as abcba
, the condition should be ptr2 >= ptr1
:
int palindrome(char* string)
{
size_t len = strlen(string);
// handle empty string and string of length 1:
if (len == 0) return 0;
if (len == 1) return 1;
char *ptr1 = string;
char *ptr2 = string + len - 1;
while(ptr2 >= ptr1) {
if (!isalpha(*ptr2)) {
ptr2--;
continue;
}
if (!isalpha(*ptr1)) {
ptr1++;
continue;
}
if( tolower(*ptr1) != tolower(*ptr2)) {
return 0;
}
ptr1++; ptr2--;
}
return 1;
}
you might need to #include <ctype.h>