The tolower()
function in C++ converts a given character to lowercase. It is defined in the cctype header file.
Example
#include <iostream>
#include <cctype>
using namespace std;
int main() {
// convert 'A' to lowercase
char ch = tolower('A');
cout << ch;
return 0;
}
// Output: a
tolower() Syntax
The syntax of the tolower()
function is:
tolower(int ch);
tolower() Parameters
The tolower()
function accepts the following parameter:
- ch - a character, casted to
int
type orEOF
tolower() Return Value
The tolower()
function returns:
- For Alphabets - the ASCII code of the lowercase version of ch
- For Non-Alphabets - the ASCII code of ch
tolower() Prototype
The function prototype of tolower()
as defined in the cctype header file is:
int tolower(int ch);
As we can see, the character argument ch is converted to int
i.e. its ASCII code.
Since the return type is also int
, tolower()
returns the ASCII code of the converted character.
tolower() Undefined Behavior
The behaviour of tolower()
is undefined if:
- the value of ch is not representable as unsigned char, or
- the value of ch is not equal to
EOF
.
Example 1: C++ tolower()
#include <cctype>
#include <iostream>
using namespace std;
int main() {
char c1 = 'A', c2 = 'b', c3 = '9';
cout << (char) tolower(c1) << endl;
cout << (char) tolower(c2) << endl;
cout << (char) tolower(c3);
return 0;
}
Output
a b 9
Here, we have converted the characters c1, c2, and c3 to lowercase using tolower()
.
Notice the code for printing the output:
cout << (char) tolower(c1) << endl;
Here, we have converted the return value of tolower(c1)
to char
using the code (char) tolower(c1)
.
Also notice that initially:
c2 = 'b'
and sotolower()
returns the same valuec3 = '9'
and sotolower()
returns the same value
Example 2: C++ tolower() without Type Conversion
#include <cctype>
#include <iostream>
using namespace std;
int main() {
char c1 = 'A', c2 = 'b', c3 = '9';
cout << tolower(c1) << endl;
cout << tolower(c2) << endl;
cout << tolower(c3);
return 0;
}
Output
97 98 57
Here, we have converted the characters c1, c2, and c3 to lowercase using tolower()
.
However, we have not converted the returned values of tolower()
to char
. So, this program prints the ASCII values of the converted characters, which are:
97
- the ASCII code of'a'
98
- the ASCII code of'b'
57
- the ASCII code of'9'
Example 3: C++ tolower() with String
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "John is from USA.";
char ch;
cout << "The lowercase version of \"" << str << "\" is " << endl;
for (int i = 0; i < strlen(str); i++) {
// convert str[i] to lowercase
ch = tolower(str[i]);
cout << ch;
}
return 0;
}
Output
The lowercase version of "John is from USA." is john is from usa.
Here, we have created a C-string str with the value "John is from USA."
.
Then, we converted all the characters of str to lowercase using a for loop. The loop runs from i = 0
to i = strlen(str) - 1
.
for (int i = 0; i < strlen(str); i++) {
...
}
In other words, the loop iterates through the whole string since strlen() gives the length of str.
In each iteration of the loop, we convert the string element str[i] (a single character of the string) to lowercase and store it in the char
variable ch.
ch = tolower(str[i]);
We then print ch inside the loop. By the end of the loop, the entire string has been printed in lowercase.
Also Read: