Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
“A man, a plan, a canal: Panama” is a palindrome.
“race a car” is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
bool isPalindrome(char* s) {
int i,j;
if(!s) return true;
j=strlen(s)-1;
i=0;
while(i<j)
{
if(s[i]>='A' && s[i]<='Z') s[i]+=32;
if(s[j]>='A' && s[j]<='Z') s[j]+=32;
if(s[i]<48 || s[i]>122 || (s[i]>57 && s[i]<97) ) {i++;continue;}
if(s[j]<48 || s[j]>122 || (s[j]>57 && s[j]<97)) {j--;continue;}
if(s[i]!=s[j]) break;
i++,j--;
}
if(i<j) return false;
return true;
}
注意忽略非数字字母即可。