c++判断字符串是否回文
时间: 2023-12-13 14:33:22 浏览: 58
以下是C++实现判断字符串是否回文的代码示例:
```c++
#include <iostream>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;
bool isPalindrome(string s) {
stack<char> st;
int len = s.length();
for (int i = 0; i < len; i++) {
if (isalnum(s[i])) {
st.push(tolower(s[i]));
}
}
for (int i = 0; i < len; i++) {
if (isalnum(s[i])) {
if (tolower(s[i]) != st.top()) {
return false;
}
st.pop();
}
}
return true;
}
int main() {
string s = "A man, a plan, a canal: Panama";
if (isPalindrome(s)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
```