Convert a String In C++ To Upper Case

后端 未结 30 1505
一个人的身影
一个人的身影 2020-11-22 05:25

How could one convert a string to upper case. The examples I have found from googling only have to deal with chars.

30条回答
  •  北海茫月
    2020-11-22 06:04

    //works for ASCII -- no clear advantage over what is already posted...
    
    std::string toupper(const std::string & s)
    {
        std::string ret(s.size(), char());
        for(unsigned int i = 0; i < s.size(); ++i)
            ret[i] = (s[i] <= 'z' && s[i] >= 'a') ? s[i]-('a'-'A') : s[i];
        return ret;
    }
    

提交回复
热议问题