C++ tolower() Function

The cctype header file contains the definition of the tolower C++ function. If a character is passed in the function and it is an uppercase letter, then the tolower C++ method turns the uppercase letter into an equivalent lowercase letter.
Syntax of C++ tolower()
Syntax to use the tolower C++ method in a C++ program:
Declared in the cctype header file as:
Parameters of C++ tolower()
The required parameter for the tolower C++ function is a character, which has to be changed into an equivalent lowercase character.
The character parameter is auto-type-casted to the int type (ASCII value of the character) in the tolower C++ method.
Return Value of C++ tolower()
tolower C++ method returns:
- For Alphabet argument (a-z, A-Z): Returns the ASCII value for the argument's lowercase equivalent character.
- For Non-alphabets: Returns the argument's ASCII value directly. Non-alphabets can be of type special characters (%, &, @, etc.), numeric values (1, 2, 6, etc.), or anything apart from the alphabet.
Undefined Behavior of tolower() Function
The behaviour of the tolower() C++ function is undefined if the argument's value is neither an unsigned char nor equal to EOF.
Examples of tolower C++ Function
Let's look at the C++ example programs implementation of the tolower C++ function
tolower() With Type Conversion
We are converting two alphabets from uppercase to lowercase using the tolower C++ method, while two of the non-alphabets remain unchanged in the below C++ program.
Output:
Explanation: In the above program, we have declared and initialized four char variables. We are using the tolower C++ method in the above program to convert the uppercase characters into lowercase characters. We have printed the lowercase characters by typecasting the return value of the tolower() function to the char type as the tolower() function returns the ASCII codes for the equivalent lowercase characters.
tolower() Without Type Conversion
We are converting four alphabets from uppercase to lowercase using the tolower C++ method, but
Output:
Explanation: we have declared and initialized four char variables in the above program. We are using the tolower C++ method to convert the uppercase characters into lowercase characters, but we are not type-casting the value returned by the tolower function, i.e., the equivalent ASCII value. So, the output we get is the equivalent ASCII code for the corresponding char variable values.
tolower() With String
In the C++ program below, we convert a whole string (char array) into a lowercase string using the tolower C++ function.
Output:
Explanation: we have declared and initialized a char array in the above program. We are using the tolower C++ method and for loop in the above program to convert all the uppercase characters of the string into lowercase characters. We have printed the mixed upper and lowercase string and the complete lowercase string in the output.
How does C++ tolower() Work?
According to the cctype header file, the function prototype for tolower() is:
The character parameter ch is transformed into an int value, the lowercase character’s ASCII value.
The tolower() C++ function returns the lowercase character's ASCII code, so it needs to be type-casted to char to print it in the output.
Conclusion
- We have to include the cctype header file to use the tolower C++ method in a C++ program.
- The tolower C++ method takes one character parameter and converts the uppercase character to an equivalent lowercase character.
- The behaviour of the tolower() C++ function is undefined if the argument's value is neither an unsigned char nor equal to EOF.
- The return value of the tolower() C++ function must be type-casted first to char since it returns the lowercase character's ASCII code.