首页 > 解决方案 > 试图刷新我的编码。回文练习给我一个问题。谷歌搜索了我的错误,但无法修复它。

问题描述

我正在做一个 c++ 练习,老实说,关于字符串的知识有点模糊,可能是为什么这不起作用。我在语法上忘记了很多,但总的来说我对 OOP 有很好的基本理解。只是想回到事物的摇摆中。我也知道我的一致性与 std:: 很奇怪,然后在像 cin haha​​ 这样的地方省略了它。就是胡闹。谢谢你的帮助。

#include <iostream>
#include <string>
std::string reverse(std::string s);
using namespace std;

int main(){

        std::string s;
        cin>>s;

        std::string n = reverse(s);
        if(n == s){
                std::cout<<"plindrome"endl;
        }
}


string reverse(string s){

        int n = s.length();

        for(int i = 0; i<n/2; i++){
                swap(s[i], s[n-1-1])
        }

        return s;

}

标签: c++stringoopc++11palindrome

解决方案


swap(s[i], s[n-1-1])

仔细看第二个论点。


推荐阅读