C++ how-to

Convert Uppercase to Lowercase in C++

Use std::tolower for individual characters and apply it across a std::string. Cast through unsigned char before calling it to avoid undefined behavior for negative char values.

Convert a std::string safely

Include algorithm, cctype and string. Call std::transform with the same string as input and output. In the lambda, accept unsigned char, pass it to std::tolower and cast the returned integer back to char. This safely handles ordinary single-byte text without assuming plain char is unsigned.

UseInputResult
Core expression[](unsigned char ch)static_cast<char>(std::tolower(ch))
Apply in placetext.begin(), text.end()write back to text.begin()

Why unsigned char matters

The cctype functions accept EOF or a value representable as unsigned char. Passing a negative plain char produces undefined behavior. Whether char is signed depends on the compiler and target, so an explicit unsigned char conversion is important even when simple ASCII examples appear to work.

Convert one character

For one value, cast the original character to unsigned char before std::tolower, then cast the result back to char. Keep this rule inside helper functions and add tests for uppercase letters, lowercase letters, digits, punctuation and empty strings so later refactors do not remove the safety step.

UTF-8 needs a Unicode-aware solution

std::tolower uses the active C locale and does not by itself perform full Unicode case mapping over a UTF-8 string. Some characters use multiple bytes or have language-specific rules. Choose a Unicode-aware library when international text must be correct. The FixCaseNow browser converter is useful for a quick pasted result, not as a replacement for tested application logic.