person with red hair and glasses sits at a desk surrounded by interface elements

In C++, working with strings is a common operation. Whether you’re processing user input or manipulating text data, you may find the need to change the case of a string. This article will guide you through converting a string to either lower case or upper case in C++.

Using Standard Library Functions

C++ provides a rich standard library which includes functions and methods to perform these case conversions with ease. The std::string class is one of the most commonly used classes for this purpose.

Use std::transform and tolower or toupper

In C++, string manipulation is a cornerstone of many applications, ranging from user input processing to data transformations. One of the most common tasks within this realm is altering the case of characters within a string. 

  • The C++ Standard Library offers an elegant and efficient solution for this: combining std::transform from the <algorithm> header with the character conversion functions std::tolower and std::toupper from the <cctype> header;
  • The std::transform function is adept at applying a given operation to a range of elements, making it ideal for iterating through strings;
  • When paired with std::tolower or std::toupper, it can modify each character in a string to be either lowercase or uppercase, respectively.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <string>
#include <cctype>
 
std::string& to_lower(std::string& str)
{
    std::transform(str.begin(), str.end(), str.begin(), static_cast<int(*)(int)>(std::tolower));
    return str;
}
 
std::string& to_upper(std::string& str)
{
    std::transform(str.begin(), str.end(), str.begin(), static_cast<int(*)(int)>(std::toupper));
    return str;
}
 
std::string str = "Chorlton-cum-Hardy";
to_lower(str);
to_upper(str);

Use boost::algorithm::to_lower or boost::algorithm::to_upper

The Boost library in C++ provides a set of extensions that go beyond the standard library’s functionalities. Within Boost, the Algorithm library offers string algorithms that streamline many string-related operations. One notable feature is the boost::algorithm::to_lower and boost::algorithm::to_upper functions. These functions provide a straightforward way to change an entire string to lowercase or uppercase without the need to combine or iterate with other functions. They serve as a more direct alternative to the standard library methods, simplifying string case conversion in C++ code. 

#include <boost/algorithm/string.hpp>	
 
std::string str = "Adwick-le-Street";
boost::algorithm::to_lower(str);
boost::algorithm::to_upper(str);

Conclusion

String manipulation, particularly case conversion, is a foundational skill in C++ programming. As we’ve explored, the language offers both standard library tools and external solutions, like the Boost library, to aid developers in these tasks. The std::transform function combined with std::tolower or std::toupper allows for intuitive conversions using the standard library. Meanwhile, the Boost Algorithm library provides an even more direct approach with boost::algorithm::to_lower and boost::algorithm::to_upper. For those interested in further expanding their C++ proficiency, understanding how to list files in a directory is another valuable skill. You can delve deeper into this topic by reading our guide on how to list files in a directory. By mastering these techniques, one can elevate their C++ programming capabilities and handle text data with greater ease and precision. Whether you opt for the standard approach or the Boost enhancements, the ability to effectively manipulate string case is a valuable asset in any C++ developer’s toolkit.

Leave a Reply