How to convert std::string to lower case?

后端 未结 26 1739
旧时难觅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:35

    I wrote this simple helper function:

    #include  // tolower
    
    string to_lower(string s) {        
        for(char &c : s)
            c = tolower(c);
        return s;
    }
    

    Usage:

    string s = "TEST";
    cout << to_lower("HELLO WORLD"); // output: "hello word"
    cout << to_lower(s); // won't change the original variable.
    

提交回复
热议问题