- 题目:验证回文串
- 难度:简单
- 题目描述:给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
- 示例1:输入: “A man, a plan, a canal: Panama” 输出: true
- 示例2:输入: "race a car"输出: false
C语言解法:一个头指针,一个尾指针,前后对比
bool isPalindrome(char * s){
int len = strlen(s);
if (len == 0 || len == 1){
return true;
}
char *end = s;
end += len - 1;
while (end >= s){
while (end >= s){
if (*end >= 'a' && *end <= 'z' || *end >= 'A'&&*end <= 'Z' || *end >= '0' && *end <= '9'){
break;
}
else{
end--;
continue;
}
}
while (end >= s){
if (*s >= 'a' && *s <= 'z' || *s >= 'A'&&*s <= 'Z' || *s >= '0' && *s <= '9'){
break;
}
else{
s++;
continue;
}
}
if (end < s) break;
if (*end == *s || *end == toupper(*s) || *end == tolower(*s)){
end--;
s++;
continue;
}else{
return false;
}
}
return true;
}