c++分别判断多个字符串是否是回文字符串
时间: 2023-06-21 13:16:37 浏览: 156
下面是一个 C++ 程序,可以分别判断多个字符串是否是回文字符串:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
bool is_palindrome(string s) {
int n = s.length();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - i - 1]) {
return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
cin.ignore(); // 忽略输入中的换行符
for (int i = 0; i < n; i++) {
string s;
getline(cin, s);
if (is_palindrome(s)) {
cout << "The string is a palindrome" << endl;
} else {
cout << "The string is not a palindrome" << endl;
}
}
return 0;
}
```
这个程序使用了 `string` 类型来表示每个字符串,使用 `is_palindrome` 函数来判断一个字符串是否是回文字符串。在主函数中,通过循环输入多个字符串,然后调用 `is_palindrome` 函数来判断每个字符串是否是回文字符串,并输出相应的信息。
阅读全文
相关推荐















