Start learning free

LESSON 65 OF 667 · STRINGS I

Uppercase and Lowercase in C++

Lesson

In C++, there is no built-in method to convert an entire string to uppercase or lowercase. Instead, use std::transform with ::toupper or ::tolower from <algorithm>:

string s = "Hello World";

string upper = s;
transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
// upper == "HELLO WORLD"

string lower = s;
transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
// lower == "hello world"

transform applies a function to each character. ::toupper converts a character to uppercase, and ::tolower converts it to lowercase.


Assignment
Follow the Coding Tutorial and create upper and lower versions of word!


Hint
Use transform with ::toupper for uppercase and ::tolower for lowercase.

STEP 1 OF 2
    Ctrl + Enter to check