C++
String Manipulation
Programming
Lower Case Conversion
Code Tutorial

How to convert an instance of stdstring to lower case

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Converting a string to lowercase in C++ using the Standard Library (STL) is an essential task for many applications, including text processing, user input normalization, and case-insensitive comparisons. In C++, std::string is a fundamental datatype for handling and manipulating strings. However, the std::string class itself does not provide a direct method to transform its contents to lowercase. To achieve this, we have to utilize other components of the STL, particularly <locale> for localization support and <algorithm> for performing operations on ranges.

Using <algorithm> and std::tolower

The usual approach to convert a string to lowercase involves iterating over each character of the string and converting it to lowercase using the std::tolower function. This function is part of the C++ <cctype> header and works with individual characters.

Here's a step-by-step example demonstrating how to convert an std::string to lowercase:

  1. Include necessary headers: You need to include <algorithm> for the std::transform function and <cctype> for the std::tolower function.
cpp
1   #include <iostream>
2   #include <string>
3   #include <algorithm>
4   #include <cctype>
  1. Define your string: Create and initialize an std::string object.
cpp
   std::string data = "Hello, World!";
  1. Convert to lowercase: Use std::transform with std::tolower to iterate and convert each character.
cpp
   std::transform(data.begin(), data.end(), data.begin(),
                   [](unsigned char c) -> unsigned char { return std::tolower(c); });
  • std::transform takes four arguments: the beginning of the source range, the end of the source range, the beginning of the destination range, and a unary operation function.
  • The lambda function is used to ensure that each character is correctly cast to unsigned char for safe conversion, which is necessary as std::tolower can potentially produce undefined behavior if given a char with a negative value that doesn't correspond to EOF.
  1. Output the result: After the transformation, your string data will be in lowercase.
cpp
   std::cout << data << std::endl;  // Output: hello, world!

Important Tips and Considerations

  • Locale-specific conversions: The example above performs the conversion based on the C locale. For handling different locales, you might need to use a locale-specific version of std::tolower that takes a second argument of type std::locale. Remember to #include <locale> for this purpose.
  • Unicode and wide characters: The above method only works reliably for ASCII characters. For Unicode or wide-character sets, more sophisticated methods involving std::wstring or UTF-8 libraries (such as ICU) might be necessary.

Summary Table

FunctionPurposeExampleHeader
std::transformApplies a function over a range in-place.std::transform(data.begin(), data.end(), data.begin(), operation);<algorithm>
std::tolowerConverts a single character to lowercase.std::tolower(c);<cctype>

Conclusion

Converting an std::string to lowercase is straightforward with the use of <algorithm> and <cctype>. By applying std::transform combined with std::tolower, strings can be efficiently transformed in-place. For specific localization needs or character sets beyond ASCII, additional considerations regarding locales or encoding might be necessary. This approach provides a robust foundation for text manipulation tasks in C++ where case normalization is required.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.