恰巧学习了ctype.h里的函数,发现能满足需求,于是就在代码里使用了。
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
bool huiwen(string s) {
int j = 0;
for (int i = 0, j = s.size() - 1; i < j; i++, j--) {
while (isalnum(s[i]) == false && i < j) i++;
while (isalnum(s[i]) == false && i < j) j--;
if (toupper(s[i]) != toupper(s[j])) {
return false;
}
else {
return true;
}
}
}
int main()
{
string s;
cout << "Please input a string" << endl;
getline(cin, s);
if (huiwen(s) == true) {
cout << "The string is a palindrome" << endl;
}
else{
cout << "The string is not a palindrome" << endl;
}
system("pause");
return 0;
}