忽略字母的大小写,忽略非字母的字符:
bool IsPalindrome(const std::string &str)
{
if(str.size() < 2) return false;
int start = 0;
int end = str.size()-1;
while(start < end)
{
//忽略左边无效字符
while (start < end && !isalnum(str[start]))
{
++start;
}
//忽略右边无效字符
while (start < end && !isalnum(str[end]))
{
--end;
}
if(start < end)
{
if(tolower(str[start]) != tolower(str[end]))
{
return false;
}
start++;
end--;
}
}
return true;
}