Convert first letter in string to uppercase

后端 未结 5 1120
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 23:27

I have a string: \"apple\". How can I convert only the first character to uppercase and get a new string in the form of \"Apple\"?

I can al

5条回答
  •  甜味超标
    2020-12-02 23:56

    #include 
    using namespace std;
    
    void capitalize (string &s)
    {
        bool cap = true;
    
        for(unsigned int i = 0; i <= s.length(); i++)
        {
            if (isalpha(s[i]) && cap == true)
            {
                s[i] = toupper(s[i]);
                cap = false;
            }
            else if (isspace(s[i]))
            {  
                cap = true;
            }
        }
    }
    

提交回复
热议问题