1、编写程序,输入一个字符串,判断该字符串是否为回文字符串,
算法简介:
1.输入字符串利用库函数找到字符串结束位置;
2.依次将字符串左侧的字符和字符串右侧字符比较,如果字符串相同继续,不同则退出比较。
3.根据之前比较是否有不同元素判断输出最后结果。
输入一个字符串,判断该字符串是否为回文字符串(语言-c++)
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
threenewbee 2023-05-24 11:12关注#include <iostream> #include <cstring> using namespace std; bool isPalindrome(string str) { int left = 0, right = str.length() - 1; while (left < right) { if (str[left] != str[right]) { return false; } left++; right--; } return true; } int main() { string str; cout << "请输入字符串:"; cin >> str; if (isPalindrome(str)) { cout << "是回文字符串" << endl; } else { cout << "不是回文字符串" << endl; } return 0; }解决 无用评论 打赏 举报 编辑记录