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.
The Recommended Way: std::transform
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.
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 a–z 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
| Goal | Code |
|---|---|
| Uppercase a whole string | std::transform(s.begin(), s.end(), s.begin(), ::toupper) |
| Lowercase a whole string | std::transform(s.begin(), s.end(), s.begin(), ::tolower) |
| Uppercase one character | std::toupper(c) |
| Lowercase one character | std::tolower(c) |
| Required headers | <string>, <algorithm>, <cctype> |
Related Articles
- C++ String Handling — the std::string essentials
- C++ Compare Strings — case-insensitive comparison made easy
- C++ char to int — working with individual characters
- C++ String Contains a Substring — searching inside text
- C++ Range-Based For Loop — the loop used above
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.