Convert a string to lowercase in C++

In this article, we will discuss different ways to convert a string to lowercase in C++.

Table Of Contents

Method 1: Using std::tolower() & for_each()

Using STL Algorithm for_each(), we can iterate over each character of string. During iteration, for_each() function will pass the refrence of each character to the given lambda function, which intern changes the case of character to lower case. Therefore, at the end of loop, all characters in string will be in lowercase.

Let’s see an example,

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

int main()
{
    std::string sampleText = "THIS IS A Strong Text";

    // Iterate over each character in string 
    // and convert each character to lower case
    std::for_each(
        sampleText.begin(),
        sampleText.end(),
        [](char& ch){
            ch = std::tolower(ch);
        });

    std::cout << sampleText << std::endl;

    return 0;
}

Output:

this is a strong text

It converted the string to lowercase.

Method 2: Using transform() & tolower()

Using STL Algorithm transform(), we can transform over each character of string. Pass the iterators pointing to start and end of string, as first two argument in the transform() function. Then pass the iterator pointing to the start of string, as third argument i.e. output range. Also, pass a function ::tolower() as fourth argument in the transform() function.

This transform() function will iterate over each character of string, pass each character to the tolower() function. Which intern returns the lowercase version of character. Then transform() function will save it to the string again. This way all characters will become lowercase in the string.

Let’s see an example,

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

int main()
{
    std::string sampleText = "THIS IS A Strong Text";

    // Iterate over each character in string 
    // and transform each character to lower case 
    std::transform(
        sampleText.begin(),
        sampleText.end(),
        sampleText.begin(),
        ::tolower);

    std::cout << sampleText << std::endl;

    return 0;
}

Output:

this is a strong text

Output:

this is a strong text

It converted the string to lowercase.

Method 3: Using transform() & Lambda Function

Using STL Algorithm transform(), we can transform over each character of string. Pass the iterators pointing to start and end of string, as first two argument in the transform() function. Then pass the iterator pointing to the start of string, as third argument i.e. output range. Also, pass a lambda function as fourth argument in the transform() function.

This transform() function will iterate over each character of string, and pass each character to the given lambda function. Which intern returns the lowercase version of character. Then transform() function will save it to the string again. This way all characters will become lowercase in the string.

Let’s see an example,

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

int main()
{
    std::string sampleText = "THIS IS A Strong Text";

    // Iterate over each character in string 
    // and transform each character to lower case 
    std::transform(
        sampleText.begin(),
        sampleText.end(),
        sampleText.begin(),
        [](const char& ch){
            return ::tolower(ch);
        });

    std::cout << sampleText << std::endl;

    return 0;
}

Output:

this is a strong text

Output:

this is a strong text

It converted the string to lowercase.

Method 4: Using for loop

It is the basic solution. Just iterate over all characters of string using a for loop, and convert each character to lowercase using ::tolower(). Let’s see an example,

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

int main()
{
    std::string sampleText = "THIS IS A Strong Text";

    // Iterate over each character in string 
    // and change each character to lower case 
    for(char& ch: sampleText)
    {
        ch = ::tolower(ch);
    }

    std::cout << sampleText << std::endl;

    return 0;
}

Output:

this is a strong text

It converted the string to lowercase.

Summary

We learned about different ways to convert a string to lowercase in C++. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top