tolower() function in C/C++
The tolower() function is defined in the ctype.h header file. If the character passed is an uppercase alphabet then the tolower() function converts an uppercase alphabet to a lowercase alphabet.
int tolower(int ch);
Basically tolower() returns an int value and in C language we can user %c to convert that int value to char value. But in c++ typecasting is required like this:
char c = (char) tolower(‘A’);
Parameter: This method takes a mandatory parameter ch which is the character to be converted to lowercase.
Return Value: This function returns the lowercase character corresponding to the ch.
Below programs illustrate the tolower() function in C:
Example 1:-
C++
// C++ program to demonstrate// example of tolower() function.#include <iostream>using namespace std;int main(){ char c = 'G'; cout << c << " in lowercase is represented as = "; // tolower() returns an int value there for typecasting // with char is required cout << (char)tolower(c);} |
C
// C program to demonstrate// example of tolower() function.#include <ctype.h>#include <stdio.h>int main(){ // Character to be converted to lowercase char ch = 'G'; // convert ch to lowercase using toLower() printf("%c in lowercase is represented as = %c", ch, tolower(ch)); return 0;} |
G in lowercase is represented as = g
Example 2:-
C++
// C++ program to convert a string to lowercase// using tolower#include <bits/stdc++.h>using namespace std;int main(){ // string to be converted to lowercase string s = "GEEKSFORGEEKS"; for (auto& x : s) { x = tolower(x); } cout << s; return 0;}// code is contributed by triangleofcoding |
C
// C program to demonstrate// example of tolower() function.#include <ctype.h>#include <stdio.h>int main(){ int j = 0; char str[] = "GEEKSFORGEEKS\n"; // Character to be converted to lowercase char ch = 'G'; // converting every character to lowercase while (str[j]) { str[j] = tolower(str[j]); j++; } // printing the string for (int i = 0; i < j; i++) printf("%c", str[i]); return 0;} |
geeksforgeeks
Note:
If the character passed in the tolower() is any of these three
1. lowercase character
2. special symbol
3. digit
tolower() will return the character as it is.
Example 3:
C++
// C++ program to demonstrate// example of tolower() function.#include <iostream>using namespace std;int main() { string s="Geeks@123"; for(auto x:s){ cout << (char)tolower(x); } return 0;}//contributed by triangleofcoding |
C
// C program to demonstrate// example of tolower() function.#include <ctype.h>#include <stdio.h>int main(){ int j = 0; char str[] = "GeEks@123\n"; char ch; while (str[j]) { ch = str[j]; putchar(tolower(ch)); j++; } return 0;}// code is contributed by codersaty |
geeks@123


