Introduction

In C++, there are several ways to convert a string to lower case. In this tutorial, we will explore a few methods you can use to achieve this.

Method 1: Using the transform() function

The transform() function in C++ can be used to convert characters in a string to a specified case. To convert a string to lower case, we can use this function along with the std::tolower() function from the library.

Here’s an example of how you can convert a string to lower case using transform():

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

int main() {
  std::string str = ""Hello World"";
  std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); });

  std::cout << str << std::endl;  // Output: hello world

  return 0;
}

Method 2: Using a for loop

Another approach to convert a string to lower case is by iterating over each character in the string and converting it to lower case using the std::tolower() function.

Here’s an example:

#include <iostream>
#include <cctype>

int main() {
  std::string str = ""Hello World"";

  for (char& c : str) {
    c = std::tolower(c);
  }

  std::cout << str << std::endl;  // Output: hello world

  return 0;
}

Method 3: Using the boost::algorithm library

If you have the Boost library installed, you can use the boost::to_lower() function to convert a string to lower case.

Here’s an example using the boost::algorithm library:

#include <iostream>
#include <boost/algorithm/string.hpp>

int main() {
  std::string str = ""Hello World"";
  boost::algorithm::to_lower(str);

  std::cout << str << std::endl;  // Output: hello world

  return 0;
}

These are some of the common ways to convert a string to lower case in C++. You can choose the method that best fits your needs and requirements.

I hope you found this tutorial helpful!