Allow for palindromes with non-alpha characters

This commit is contained in:
Nick Chambers 2022-01-17 11:21:00 -06:00
parent 348f5d76e5
commit ad0a18e8a9
1 changed files with 14 additions and 4 deletions

View File

@ -1,8 +1,18 @@
int palindrome(const char *str, unsigned len) {
unsigned idx = 0;
#include <ctype.h>
for(; idx < (len - (len % 2)) / 2; idx += 1) {
if(str[idx] != str[len - idx - 1]) {
int palindrome(const char *str, unsigned len) {
const char *start = str;
const char *end = str + len - 1;
while(start < end) {
if(!isalpha(*start)) {
start += 1;
} else if(!isalpha(*end)) {
end -= 1;
} else if(tolower(*start) == tolower(*end)) {
start += 1;
end -= 1;
} else {
return 0;
}
}