C ++ islower()函数调试断言失败错误

时间:2018-12-20 16:37:00

标签: c++

谢谢大家的帮助,我解决了这个问题,并在最后为那些有相同问题的人提供了工作代码。

我正在尝试编写一个简单的函数,将字符串变量中的所有字母字符清除,并将变量全部转换为小写。

  

例如,
  “ h4el> lo”改为“ hello”。
  从“大”到“大”。

唯一的例外是(')撇号,因为有(不能)之类的字眼。
我编写的代码在正常情况下可以正常工作。

string CleanTheWord(string word)
{
    string cleanWord = "";

    for (int i = 0; i < word.length(); i++)
    {
        if (islower(word[i]))
        {
            cleanWord += word[i];
        }
        else if (isupper(word[i]))
        {
            cleanWord += tolower(word[i]);
        }
        else if (word[i] == 39 || word[i] == 44) //Apostrophe mark
        {
            cleanWord += word[i];
        }
    }
    return cleanWord;
}

问题是我需要将此函数应用于大量变量,即使大多数变量没有问题,某些变量也包含不常见的字符。 例如,导致“调试断言失败错误”的奇怪字符串值为:

  

she�s

我所犯的错误是:

  

调试断言失败
  程序://程序路径
  文件minkernel \ crts \ ucrt \ src \ appcrt \ convert \ isctype.cpp
  线:36
  表达式c> =-1 && c <= 255

我希望能够将“she�s”转换为“ shes”(不删除字母字符“�”)。
或者,如果不可能,我至少要忽略有问题的单词,以使程序不会崩溃并继续正常运行。

---------工作代码---------

string CleanTheWord(string word)
{
    string newWord = "";        

    for (int i = 0; i < word.length(); i++)
    {
        char control = word[i] < 0 ? 0 : word[i];

        if (isupper(control))
        {
            newWord += tolower(control);
        }
        else if (isalpha(control) || control == '\'')
        {
            newWord += control;
        }
    }

    //cout << "Final result: " << newWord << endl;
    return newWord;
}

0 个答案:

没有答案