How to Convert a C++ String to Uppercase

前端 未结 6 1742
不知归路
不知归路 2021-02-01 05:34

I need to convert a string in C++ to full upper case. I\'ve been searching for a while and found one way to do it:

#include 
#include 

        
6条回答
  •  离开以前
    2021-02-01 06:01

    #include 
    
    using namespace std;
    
    //function for converting string to upper
    string stringToUpper(string oString){
       for(int i = 0; i < oString.length(); i++){
           oString[i] = toupper(oString[i]);
        }
        return oString;
    }
    
    int main()
    {
        //use the function to convert string. No additional variables needed.
        cout << stringToUpper("Hello world!") << endl;
        return 0;
    }
    

提交回复
热议问题