tolower() function in C++

In this guide, you will learn what is tolower() function is in C++ programming and how to use it with an example.

1. tolower() Function Overview

The tolower() function is part of the C++ <cctype> library. It is used to convert a given character to its lowercase equivalent if it's an uppercase letter (A-Z). If the input character is already lowercase or not an alphabetic letter, it returns the character unchanged.

Signature:

int tolower(int ch);

Parameters:

- ch: The character to be converted. Although it's an int, it essentially represents the ASCII value of the character.

2. Source Code Example

#include <iostream>
#include <cctype>

int main() {
    char ch1 = 'A';
    char ch2 = 'z';

    std::cout << "Lowercase of ch1 (" << ch1 << ") is: " << static_cast<char>(tolower(ch1)) << std::endl;
    std::cout << "Lowercase of ch2 (" << ch2 << ") is: " << static_cast<char>(tolower(ch2)) << std::endl;

    return 0;
}

Output:

Lowercase of ch1 (A) is: a
Lowercase of ch2 (z) is: z

3. Explanation

In this example, the tolower() function is used to convert characters to their lowercase equivalents. 

For ch1, which contains the uppercase letter 'A', the function returns its lowercase equivalent 'a'. 

For ch2, which already contains a lowercase letter 'z', the function returns it unchanged.


Comments