0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert String to Uppercase Easily

In C++, you can convert a string to uppercase by using the std::transform function with ::toupper, like this: std::transform(str.begin(), str.end(), str.begin(), ::toupper);.
📋

Examples

Inputhello
OutputHELLO
InputC++ Programming
OutputC++ PROGRAMMING
Input123abc!?
Output123ABC!?
🧠

How to Think About It

To convert a string to uppercase, think of changing each letter one by one to its uppercase form. You keep all other characters the same. Using a function that applies this change to every character in the string makes this easy and fast.
📐

Algorithm

1
Get the input string.
2
For each character in the string, convert it to uppercase if it is a lowercase letter.
3
Replace the original character with the uppercase character.
4
Return or print the modified string.
💻

Code

cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::string str = "Hello, World!";
    std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::toupper(c); });
    std::cout << str << std::endl;
    return 0;
}
Output
HELLO, WORLD!
🔍

Dry Run

Let's trace the string "Hello" through the code.

1

Original string

str = "Hello"

2

Convert each character

H -> H, e -> E, l -> L, l -> L, o -> O

3

Final string

str = "HELLO"

Original CharUppercase Char
HH
eE
lL
lL
oO
💡

Why This Works

Step 1: Using std::transform

The std::transform function applies a given operation to each element in a range, here each character in the string.

Step 2: toupper function

The ::toupper function converts a single character to uppercase if it is lowercase; otherwise, it returns the character unchanged.

Step 3: In-place modification

By passing the string's begin iterator as both input and output, the string is modified directly without extra memory.

🔄

Alternative Approaches

Manual loop with toupper
cpp
#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string str = "Hello, World!";
    for (char &c : str) {
        c = std::toupper(static_cast<unsigned char>(c));
    }
    std::cout << str << std::endl;
    return 0;
}
This method is simple and clear but uses an explicit loop instead of a standard algorithm.
Using C++20 ranges (if available)
cpp
#include <iostream>
#include <string>
#include <ranges>
#include <cctype>

int main() {
    std::string str = "Hello, World!";
    // Using std::ranges::transform to convert to uppercase
    std::ranges::transform(str, str.begin(), [](unsigned char c){ return std::toupper(c); });
    std::cout << str << std::endl;
    return 0;
}
Requires C++20 support; more modern but less common currently.

Complexity: O(n) time, O(1) space

Time Complexity

The algorithm processes each character once, so the time grows linearly with the string length.

Space Complexity

The conversion is done in-place, so no extra space proportional to input size is needed.

Which Approach is Fastest?

Using std::transform with ::toupper is efficient and concise; manual loops are similar in speed but less elegant.

ApproachTimeSpaceBest For
std::transform with ::toupperO(n)O(1)Clean and efficient uppercase conversion
Manual loop with toupperO(n)O(1)Simple and explicit control
C++20 std::ranges::transformO(n)O(1)Modern C++ with ranges support
💡
Use std::transform with ::toupper for a clean and efficient uppercase conversion.
⚠️
Forgetting to include cctype header or using toupper without casting to unsigned char can cause unexpected behavior.