算法思路:
(1)首先检验算法为字母:
bool IsLetter(char ch) {//判读是不是字母
if (ch >= 'a' && ch <= 'z'
|| ch >= 'A' && ch <= 'Z') {
return true;
}
else
{
return false;
}
}
(2)判断是否为回文串:首先检验是否为字符,如果是进行前后对比,如果相等比较下一对字符,直到比较到字符串中间;不是字符则跳到下一个字符进行对比
#include<iostream>
#include<string>
using namespace std;
bool IsLetter(char ch) {
if (ch >= 'a' && ch <= 'z'
|| ch >= 'A' && ch <= 'Z') {
return true;
}
else
{
return false;
}
}
bool IsSymmetry(const string s) {
size_t left = 0;
size_t right = s.size()-1;
while (left < right) {
if (!IsLetter(s[right])) {
right--;
break;
}
else if (!IsLetter(s[left])) {
left++;
break;
}
if (left < right) {
if (s[left] == s[right]) {
left++;
right--;
}
else {
return false;
}
}
if (!(left < right)) {
return true;
}
}
}
int main() {
string s("hello olleh");
if (IsSymmetry(s)) {
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return 0;
}