0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert String to Lowercase Easily

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

Examples

InputHELLO
Outputhello
InputHello World!
Outputhello world!
Input123 ABC xyz!
Output123 abc xyz!
🧠

How to Think About It

To convert a string to lowercase, think of changing each letter one by one to its lowercase form. You can do this by going through the string from start to end and replacing each uppercase letter with its lowercase equivalent, leaving other characters unchanged.
📐

Algorithm

1
Get the input string.
2
For each character in the string, convert it to lowercase if it is an uppercase letter.
3
Replace the original character with the lowercase 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(), ::tolower);
    std::cout << str << std::endl;
    return 0;
}
Output
hello world!
🔍

Dry Run

Let's trace the string "Hello World!" through the code

1

Initial string

str = "Hello World!"

2

Convert each character

H -> h, e -> e, l -> l, l -> l, o -> o, ' ' -> ' ', W -> w, o -> o, r -> r, l -> l, d -> d, ! -> !

3

Final string

str = "hello world!"

Original CharConverted Char
Hh
ee
ll
ll
oo
Ww
oo
rr
ll
dd
!!
💡

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: Using ::tolower

The ::tolower function converts a single character to lowercase if it is uppercase; 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 tolower
cpp
#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string str = "Hello World!";
    for (char &c : str) {
        c = std::tolower(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 a new string with std::transform
cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::string str = "Hello World!";
    std::string lower_str;
    lower_str.resize(str.size());
    std::transform(str.begin(), str.end(), lower_str.begin(), ::tolower);
    std::cout << lower_str << std::endl;
    return 0;
}
This creates a new lowercase string, keeping the original unchanged, but uses extra memory.

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

Time Complexity

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

Space Complexity

The conversion is done in-place, so no extra space beyond the input string is needed.

Which Approach is Fastest?

Using std::transform in-place is generally fastest and most readable; manual loops are similar but more verbose; creating a new string uses extra memory.

ApproachTimeSpaceBest For
std::transform in-placeO(n)O(1)Efficient and clean lowercase conversion
Manual loop with tolowerO(n)O(1)Simple and explicit control
std::transform to new stringO(n)O(n)Preserving original string
💡
Use std::transform with ::tolower for a clean and efficient lowercase conversion.
⚠️
Forgetting to include <cctype> or using tolower without std:: or :: can cause errors.