试图刷新我的编码。回文练习给我一个问题。谷歌搜索了我的错误,但无法修复它。
Posted
技术标签:
【中文标题】试图刷新我的编码。回文练习给我一个问题。谷歌搜索了我的错误,但无法修复它。【英文标题】:Trying to refresh my coding. Palindrome excercise giving me an issue. Googled my error but cant fix it. 【发布时间】:2018-08-11 14:51:46 【问题描述】:我正在做一个 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;
【问题讨论】:
那么错误是什么? 【参考方案1】:#include <iostream>
#include <string>
std::string reverse(std::string s);
using namespace std;
string reverse(string s)
int n = s.length();
for (int i = 0; i<n / 2; i++)
swap(s[i], s[n - i - 1]);
return s;
int main()
std::string s;
cin >> s;
std::string n = reverse(s);
if (n == s)
std::cout << "plindrome" << endl;
刚刚修复了交换行,所以它应该是 s[i] 和 s[n - i - 1] 以及打印时的语法错误
【讨论】:
【参考方案2】:swap(s[i], s[n-1-1])
仔细查看第二个参数。
【讨论】:
以上是关于试图刷新我的编码。回文练习给我一个问题。谷歌搜索了我的错误,但无法修复它。的主要内容,如果未能解决你的问题,请参考以下文章