How to convert std::string to lower case?

后端 未结 26 1669
旧时难觅i
旧时难觅i 2020-11-22 00:01

I want to convert a std::string to lowercase. I am aware of the function tolower(), however in the past I have had issues with this function and it

26条回答
  •  感情败类
    2020-11-22 00:26

    Copy because it was disallowed to improve answer. Thanks SO


    string test = "Hello World";
    for(auto& c : test)
    {
       c = tolower(c);
    }
    

    Explanation:

    for(auto& c : test) is a range-based for loop of the kind
    for (range_declaration:range_expression)loop_statement:

    1. range_declaration: auto& c
      Here the auto specifier is used for for automatic type deduction. So the type gets deducted from the variables initializer.

    2. range_expression: test
      The range in this case are the characters of string test.

    The characters of the string test are available as a reference inside the for loop through identifier c.

提交回复
热议问题