Converting String Case in C++

What's the best way to convert all characters in a std::string to uppercase or lowercase?

Converting the case of characters in a std::string is a common operation in text processing. C++ provides functions in the <algorithm> and <cctype> headers to accomplish this task efficiently.

Here's how you can convert a string to uppercase or lowercase:

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
  std::string text{"Hello, World!"};

  // Convert to uppercase
  std::string uppercase{text};
  std::ranges::transform(
    uppercase, uppercase.begin(),
    [](unsigned char c) {
      return std::toupper(c);
    });  

  // Convert to lowercase
  std::string lowercase{text};
  std::ranges::transform(
    lowercase, lowercase.begin(),
    [](unsigned char c) {
      return std::tolower(c);
    });  

  std::cout << "Original: " << text << '\n';
  std::cout << "Uppercase: " << uppercase << '\n';
  std::cout << "Lowercase: " << lowercase << '\n';
}
Original: Hello, World!
Uppercase: HELLO, WORLD!
Lowercase: hello, world!

Let's break down the approach:

  1. We use std::ranges::transform() (C++20) to apply a transformation to each character.
  2. The lambda function [](unsigned char c){ return std::toupper(c); } is used to convert each character to uppercase. We use unsigned char to avoid undefined behavior with negative char values.
  3. std::toupper() and std::tolower() from <cctype> are used for the actual character conversion.

If you're using a pre-C++20 compiler, you can use std::transform() instead:

std::transform(
  text.begin(), text.end(), text.begin(),
  [](unsigned char c) {
    return std::toupper(c);
  }
);

Note that this method works well for ASCII characters, but it may not handle Unicode characters correctly. For proper Unicode support, you might need to use a library like ICU (International Components for Unicode).

Also, be aware that these functions modify the string in-place. If you need to preserve the original string, make a copy before transforming, as shown in the example.

Remember, when working with std::string, it's often more efficient to modify the string in-place rather than creating a new string for each operation. This approach minimizes memory allocations and copies, leading to better performance, especially for longer strings or frequent operations.

A Deeper Look at the std::string Class

A detailed guide to std::string, covering the most essential methods and operators

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Efficient String Concatenation in Loops
How can I efficiently concatenate multiple strings in a loop without excessive memory allocation?
Splitting a String into a Vector
How can I split a std::string into a vector of substrings based on a delimiter?
Removing Whitespace from a String
What's the most efficient way to remove all whitespace from a std::string?
Case-Insensitive String Comparison
How can I implement a case-insensitive string comparison using std::string?
Replacing All Substrings in a String
What's the best approach to replace all occurrences of a substring within a std::string?
Reversing a String in C++
What's the most efficient way to reverse a std::string?
Validating Email Addresses with Regex
How can I check if a std::string is a valid email address using regular expressions?
Checking if a String is a Palindrome
How can I efficiently check if a std::string is a palindrome?
Implementing Basic Autocomplete
How can I implement a basic autocomplete feature using a list of std::strings?
Calculating Levenshtein Distance
How can I efficiently calculate the Levenshtein distance between two std::strings?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant