Skip to content
C++ Better Explained
Go back
Convert a String to Uppercase or Lowercase in C++
Edit page

Convert a String to Uppercase or Lowercase in C++

Changing the case of a string is one of the most common things you’ll do with text — normalising user input, comparing names, formatting output. C++ doesn’t have a one-call .toUpperCase(), but the standard library gives you a clean, idiomatic way to do it in a single line.


std::transform applies a function to every character in the string. Pair it with ::toupper and you get an in-place uppercase conversion:

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

int main() {
    std::string text = "Hello, World!";
    std::transform(text.begin(), text.end(), text.begin(), ::toupper);
    std::cout << text << "\n";   // HELLO, WORLD!
    return 0;
}

The first two arguments say “read every character,” the third says “write the result back into the same string,” and ::toupper is the transformation applied to each one. Punctuation and spaces are left untouched because toupper only changes letters.


Converting to Lowercase

Lowercase is identical — just swap in ::tolower:

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

int main() {
    std::string text = "Hello, World!";
    std::transform(text.begin(), text.end(), text.begin(), ::tolower);
    std::cout << text << "\n";   // hello, world!
    return 0;
}

This symmetry is handy: the same pattern handles both directions, so you only have to remember one technique.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

A Simple Loop Version

If std::transform looks intimidating, a range-based for loop does the same job and is easy to read. Note the static_cast<unsigned char> — it’s the safe way to call toupper, because passing a negative char value is undefined behaviour:

#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string name = "ada lovelace";
    for (char& c : name)
        c = std::toupper(static_cast<unsigned char>(c));
    std::cout << name << "\n";   // ADA LOVELACE
    return 0;
}

The char& (a reference) means we modify each character in place. Drop the & and you’d only change a copy, leaving the original string untouched — a subtle but common mistake.


Why Not Just Subtract 32?

You’ll often see the “trick” of subtracting 32 from a character to capitalise it, since 'a' and 'A' sit exactly 32 apart in ASCII. Avoid it. It silently corrupts anything that isn’t a plain az letter — digits, punctuation, and especially accented or international characters all get mangled. toupper and tolower were written precisely to handle those edge cases, so let them do the work.


Quick Reference

GoalCode
Uppercase a whole stringstd::transform(s.begin(), s.end(), s.begin(), ::toupper)
Lowercase a whole stringstd::transform(s.begin(), s.end(), s.begin(), ::tolower)
Uppercase one characterstd::toupper(c)
Lowercase one characterstd::tolower(c)
Required headers<string>, <algorithm>, <cctype>


Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
How to Get a Substring in C++ with substr()
Next Post
C++ break and continue: Control Your Loops (with Examples)