C++ Program to convert uppercase String to lowercase

In this C++ Program Tutorial we will explore how to convert a C++ String from uppercase to lowercase using a variety of different methods available to us.

See Also: How to convert lowercase to uppercase in C++


Uppercase to Lowercase – Method#1

At the heart of it all, a character is merely an integer value representing a character from the ASCII table. If one were to study the ASCII table, you will observe a pattern between the Uppercase and Lowercase variants of the characters.

If we add 32 into the integer value of any Uppercase character, we will obtain it’s lowercase variant. Subtracting 32 from lowercase will give the same effect in reverse.

#include <iostream>
#include <string>
using namespace std;


int main() {
    char a = 'A';
    char b = 'B';
    char c = 'C';

    cout << a + 32 << endl;
    cout << b + 32 << endl;
    cout << c + 32 << endl;
}

The ASCII values of the uppercase versions of the a, b and c characters.

97
98
99

If you want to print out the values in the character versions, you can either use typecasting as shown in the example below, are store the updated integer (ascii) value into a char variable, and then print it out.

#include <iostream>
#include <string>
using namespace std;


int main() {
    char a = 'A';
    char b = 'B';
    char c = 'C';

    cout << (char) (a + 32) << endl;
    cout << (char) (b + 32) << endl;
    cout << (char) (c + 32) << endl;
}

The output in lowercase:

a
b
c

By simply iterating over an entire string, and adding 32 into all characters from a – z, we can convert any Uppercase string to Lowercase.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string str = "HELLOWORLD";

    for (int i = 0; i < str.length(); i++) {
        str[i] = str[i] + 32;
        cout << str[i];
    }
}

The output of the above code:

helloworld

Method#2 – tolower() function

A newer and better alternative for converting Uppercase Strings to Lowercase is the tolower() function.

In this example, we will take a look at how to convert some simple characters in C++ to lowercase using the tolower() function. You can pass both lowercase and uppercase characters into it.

int main() {
    char a = 'A';
    char b = 'B';
    char c = 'C';

    cout << tolower(a) << endl;
    cout << tolower(b) << endl;
    cout << tolower(c) << endl;
}

The output:

97
98
99

If you want the output to be in the form of actual characters, you can use typecasting, and convert the int value to its’ character representation.

int main() {
    char a = 'A';
    char b = 'B';
    char c = 'C';

    cout << (char) tolower(a) << endl;
    cout << (char) tolower(b) << endl;
    cout << (char) tolower(c) << endl;
}

The output:

a
b
c

In this example we will take a String with Uppercase characters and convert it to a Lowercase string.

The tolower() function does not work on strings natively, but remember that a String is merely a collection of characters. Hence, with the following approach, where iterate over each individual character in a String, we can convert it to lowercase.

#include <iostream>
#include <string>
using namespace std;


int main() {
    string var = "HELLO WORLD";

    for (int i = 0; i < var.length(); i++) {
        var[i] = tolower(var[i]);
    }

    cout << "New String: " << var << endl;
}

The output:

New String: hello world

This marks the end of the C++ Program to Convert Uppercase String to Lowercase Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content will be asked in the comments section below.

Leave a Comment