问题描述
我到处搜索以尝试回答自己的问题,但是我在这里碰壁。我已经在同一天进行了三天的练习,却感到沮丧,因此我的第一篇文章!这是学校的作业,但我真的很想了解为什么。当我使用输入“ bob”时,它会按预期返回“ bob是回文”。当我输入“ bobby”时,它按预期返回“ bobby不是回文”。那里一切都很好。我花了很长时间才弄清楚如何在使用“从不奇数或偶数”这个句子时删除输入中的空格,但是我也成功地做到了这一点。但是这里有问题:(1)即使删除了空格,似乎仍认为“ neveroddoreven”不是回文-为什么?我想念什么?另外,这可能是一个愚蠢的问题(但这是我第一次尝试编程,而且我是一个新手),在删除最终输出中的空格之前,如何获取它以输出原始userInput?当前,以下代码输出“ neveroddoreven不是回文”。预先感谢您可以给我的任何指点。
#include <string>
#include <cctype>
using namespace std;
int main() {
string userInput;
int startInput;
bool isPalindrome = true;
getline (cin,userInput);
startInput = userInput.length();
for(int i = 0; i<userInput.length(); i++)
if(userInput[i] == ' ') userInput.erase(i,1);
for (int i = 0; i<(startInput / 2); i++){
if (userInput[i] != userInput[(startInput -1 ) -i])
isPalindrome = false;
}
if (isPalindrome){
cout << userInput << " is a palindrome" << endl;
}
else {
cout << userInput << " is not a palindrome" << endl;
}
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)