好人 2023-05-24 11:06 采纳率: 0%
浏览 35

输入一个字符串,判断该字符串是否为回文字符串(语言-c++)

1、编写程序,输入一个字符串,判断该字符串是否为回文字符串,
算法简介:  
1.输入字符串利用库函数找到字符串结束位置; 
2.依次将字符串左侧的字符和字符串右侧字符比较,如果字符串相同继续,不同则退出比较。
3.根据之前比较是否有不同元素判断输出最后结果。

  • 写回答

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;
    }
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 请采纳用户回复 5月24日
  • 创建了问题 5月24日