0

Thank you to everybody for the help, I solved the problem and I included the working code on the end for the ones who have the same problem.

I am trying to write a simple function which cleans the string variable from none letter characters and turn the variable to all lowercase.

For example,
"h4el>lo" to "hello".
"BIG" to "big".

The only exception is (') apostrophe mark because of the words like (can't).
The code I wrote work perfectly in normal conditions.

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;
}

Problem is I need to apply this function to some big amount of variables and even if there is no problem with most of it, some variables contain uncommon characters. For example, the weird string value which causes the "Debug Assertion Failed Error" is:

she�s

And the error I am taking is:

Debug Assertion Failed
program: //program path
File minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp
Line: 36
expression c>=-1 && c <=255

I want to be able to convert "she�s" to "shes" (removing none letter characters "�").
Or if it is not possible I want to at least ignore problematic words so the program will not crash and continue as normal.

--------- Working Code ---------

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;
}
Utkan
  • 129
  • 12
  • *"I want to be able to convert "she�s" to "shes"."* That part I don't really understand tbh. Best to fix the obvious problem pointed out by the dupe I guess and then try again to re-ask this more clearly as a new question. – Baum mit Augen Dec 20 '18 at 16:41
  • Note that you don't need the `islower` and `isupper` check, as `tolower` will not do anything with lower-case characters. Use `isalpha` instead, and always call `tolower`. Also don't use [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)). If by e.g. `39` you mean the single-quote character `'\''` then use the character instead. – Some programmer dude Dec 20 '18 at 16:42
  • Just as a side remark, avoid appending to a string repeatedly since this is quite inefficient. Instead try to use for example a StringStream – lazyguy Dec 20 '18 at 17:12
  • 1
    @lazyguy I'm actually not so sure about this. I would expect the operator to be implemented in terms of `push_back`, which amortized constant due to intelligent preallocation. – Baum mit Augen Dec 20 '18 at 17:18
  • @BaummitAugen it seems you are right, according to https://en.cppreference.com/w/cpp/string/basic_string/operator%2B%3D the operation has constant cost. It seems I still had the java string implementation in mind, didn't know C++ did it better – lazyguy Dec 21 '18 at 09:56
  • @Someprogrammerdude Thank you for the advice, I changed the code as you adviced. – Utkan Dec 21 '18 at 18:34
  • @BaummitAugen I already solved the problem but for avoiding confusions, I added an explanation to that part. – Utkan Dec 21 '18 at 18:37

0 Answers0