Allow for palindromes with non-alpha characters
This commit is contained in:
parent
348f5d76e5
commit
ad0a18e8a9
1 changed files with 14 additions and 4 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue