为什么不是程序输出真不区分大小写?

问题描述 投票:-1回答:2

我的任务要求我不断从用户接受和输出输入是否是回文,直到DONE字便被输入。此外,像鲍勃字必须具有真正的输出,因为我们必须忽略大小写(上/下)。

这是一个用C ++我的第一次。

#include <iostream>
#include <string>
using namespace std;
int main()
{
string wordInput;
while (wordInput != "DONE")
{ 
    cout << "Please enter a word: ";
    cin >> wordInput;
    int wordLength = wordInput.length();
    int wordHalf = (wordLength / 2);
    bool flag = false;
    for (int i = 0; i <wordHalf; i++)
    {
        if (tolower((wordInput[i]) == tolower(wordInput[wordLength-1-i])))
        {
            flag = true;
        }
        else
        {
            flag = false;
            break;
        }
    }
    if (flag == true)
    {
        cout << "true"<<endl;
    }
    else
    {
        cout << "false"<<endl;
    }
}   
return 0;   
}
c++ palindrome
2个回答
1
投票

它可能有一些做“wordInput”正在申报两次,而循环前并在一次一次的权利。它混合了什么while循环条件看。


1
投票

你的文字没有被正确识别的问题来源于此行:

if(tolower((wordInput[i]) == tolower(wordInput[wordLength-1-i])))

如果你仔细观察,你错误地设置了括号。试试这个:

if(tolower(wordInput[i]) == tolower(wordInput[wordLength-1-i]))
© www.soinside.com 2019 - 2024. All rights reserved.