描述输入一个字符串输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。输入描述输入为一行字符串字符串中没有空白字符字符串长度不超过100。输出描述如果字符串是回文输出yes;否则输出no。希望能用c++编写代码不要讲解。以下是用例输入:abcdedcba输出:yes
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string str) {
int len = str.length();
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
return false;
}
}
return true;
}
int main() {
string str;
cin >> str;
if (isPalindrome(str)) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/i8Jk 著作权归作者所有。请勿转载和采集!