c++ string toupper code example

Example 1: uppercase capitalise character in string c++

str[i] = toupper(str[i]);

Example 2: string to upper c++

std::string data = "This is a sample string.";
// convert string to upper case
std::for_each(data.begin(), data.end(), [](char & c){
c = ::toupper(c);
});

Example 3: toupper c++

int result = toupper(charecterVariable);// return the int that corresponding upper case char
//if there is none then it will return the int for the original input.
//can convert int to char after
char result2 = (char)toupper(variableChar);

Example 4: convert all strings in vector to lowercase or uppercase c++

for(std::string &s : stringVector){
    std::transform(s.begin(), s.end(), s.begin(), 
        [](char c){ return std::toupper(c); }); // for lowercase change "toupper" -> "tolower" 
}

Tags:

Cpp Example