Overview
The tolower C++ function is implemented in the cctype header file. It transforms an uppercase letter to its corresponding lowercase letter when provided with an uppercase character as input.
Syntax:
We will implement the syntax below in a C++ program to utilize the tolower function:
tolower(<character>);
The parameters of the C++ tolower function are specified in the cctype header file.
int tolower(int ch);
C++ parameters of tolower
The tolower C++ function necessitates a character input, which needs to be transformed into a corresponding lowercase character.
In a simpler C++ approach, the character parameter undergoes an automatic type conversion to an integer type, specifically the ASCII value of the character.
Return Value of C++ tolower
The tolower C++ function returns:
Returns the ASCII code for the lowercase representation of the provided character argument within the range of the Alphabet parameter (a-z, A-Z).
Returns the ASCII value of the argument directly for non-alphabetic characters. Non-alphabetic characters encompass a wide range of symbols including special characters like %, &, and @, numerical values such as 1, 2, and 6, as well as other special characters.
Tolower Function Undefined Behavior
If the argument's value is not an unsigned char or EOF, the behavior of the tolower function in C++ is unspecified.
Examples of C++ functions for tolower
Let's explore the implementation of the tolower function in various example C++ codes.
With Type Conversion, tolower
Utilizing the tolower C++ function, two letters in uppercase are transformed to lowercase within the C++ code, while two non-alphabetic characters remain unchanged.
For Example:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch1 = 'P';
char ch2 = 'Q';
char ch3 = '7';
char ch4 = '@';
cout<< (char) tolower(ch1) << endl;
cout<< (char) tolower(ch2) << endl;
cout<< (char) tolower(ch3) << endl;
cout<< (char) tolower(ch4) << endl;
return 0;
}
Output
p
q
7
@
Explanation:
We've defined and set up four character variables in the program above. This program transforms uppercase letters to lowercase letters by utilizing the tolower function in C++. As tolower returns ASCII values for equivalent lowercase letters, we display the lowercase letters by casting its output to the char data type.
Without Type Conversion, tolower
Applying the tolower C++ function allows us to convert the case of four letters from uppercase to lowercase without the need for type conversion.
For Example:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch1 = 'B';
char ch2 = 'Y';
char ch3 = '2';
char ch4 = '@';
cout<< tolower(ch1) << endl;
cout<< tolower(ch2) << endl;
cout<< tolower(ch3) << endl;
cout<< tolower(ch4) << endl;
return 0;
}
Output
98
121
50
64
Explanation:
We have defined and assigned values to four character variables in the preceding code. Within the code, we are transforming uppercase characters into lowercase characters by employing the tolower function in C++, without explicitly converting the returned value of the tolower function, which corresponds to the ASCII value. Consequently, we receive the ASCII code equivalent to the respective values of the character variables.
Tolower Using a String
The tolower C++ function is employed in the following program to convert an entire string (character array) to lowercase.
For Example:
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char *str = "Saswat is from the INDIA";
char ch;
cout << "this string is mixed with upper and lower case \"" <<str<< "\"" << endl;
cout<< "it is pure lower case string: \"";
for (int i = 0; i < strlen(str); i++)
{
ch = tolower(str[i]);
cout<< ch;
}
cout<< "\"";
return 0;
}
Output
this string is mixed with upper and lower case "Saswat is from the INDIA"
it is pure lower case string: "saswat is from the india"
Explanation:
A character array was declared and initialized in the program above. This program employs the tolower C++ function in conjunction with a for loop to transform all uppercase characters in the string to lowercase. The result displays both the original string with a mix of upper- and lowercase characters, as well as the fully lowercase version of the string.
Conclusion
- In order to utilise the tolower C++ method in a C++ program , the cctype header file must be included.
- The uppercase character is changed to an equivalent lowercase character using the tolower C++ method, which accepts one character parameter.
- If the argument's value is neither an unsigned char nor EOF , the tolower C++ function's behaviour is undefined.
- Since the tolower C++ method returns the ASCII code for the lowercase character, it must first be type-casted to char.