Guide to tolower() Function in C with Examples

Guide to tolower() Function in C with Examples

3 mins read505 Views Comment
Updated on Aug 13, 2024 14:00 IST

Enhance your C programming skills with our detailed guide on how to use toupper() function in C effectively.

tolower function in c

In this tutorial, we will discuss the tolower() C function for converting uppercase C strings into lowercase.

Introduction to tolower() Function in C

The tolower() function is a standard library function in the C programming language that converts a given character to its lowercase equivalent.

The tolower() function is part of the ctype.h header file in C, which provides a collection of functions for character handling. It is commonly for string manipulation and text processing when it comes to C programming, particularly when dealing with user input, where case sensitivity can be an issue.

Here is the syntax for the tolower() function:


 
int tolower(int c);
Copy code

The function takes a single argument c, an integer representing the character to convert. If the argument is an uppercase letter, tolower() returns the corresponding lowercase letter. And when the argument is already a lowercase letter, it returns unchanged. Or, if the argument is not a letter, the function returns the argument as it is.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

โ‚น4.24 K
6 weeks
โ‚น475
6 weeks
โ€“ / โ€“
45 hours
โ‚น4 K
80 hours
โ€“ / โ€“
1 month
โ€“ / โ€“
40 hours
โ€“ / โ€“
2 months
โ€“ / โ€“
70 hours
โ€“ / โ€“
1 year
โ‚น4 K
80 hours

C Examples of tolower() Function 

Example 1: Converting a single character to lowercase


 
#include <stdio.h>
#include <ctype.h>
int main() {
char c = 'A';
char lc = tolower(c);
printf("The lowercase of %c is %c\n", c, lc);
return 0;
}
Copy code

Output

The lowercase of A is a

In this example, the tolower() function converts the uppercase character โ€˜Aโ€™ to its lowercase equivalent. The variable c is first initialized with the uppercase character โ€˜Aโ€™, and then passed as an argument to the tolower() function. The resulting lowercase character is stored in the lc variable, and then printed on the screen.

Example 2: Converting a string to lowercase


 
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[] = "HELLO WORLD";
int len = strlen(str);
for (int i = 0; i < len; i++) {
str[i] = tolower(str[i]);
}
printf("The lowercase of the string is %s\n", str);
return 0;
}
Copy code

Output

The lowercase of the string is hello world

In this example, the tolower() function converts each uppercase character in a given string to its lowercase equivalent. The str variable is first initialized with the string โ€œHELLO WORLDโ€, and then each character in the string is passed as an argument to the tolower() function using a for loop. The resulting lowercase character then prints to the screen using the printf() function. 

Example 3: Converting user input to lowercase


 
#include <stdio.h>
#include <ctype.h>
int main() {
char input[100];
printf("Enter a string: ");
fgets(input, 100, stdin); // read user input
// convert the string to lowercase
for(int i = 0; input[i]; i++){
input[i] = tolower(input[i]);
}
printf("The lowercase string is: %s", input);
return 0;
}
Copy code

Output:

Enter a string: Tyre

The lowercase string is: tyre

In this code, the user is prompted to enter a string. The fgets() function reads the input from the user, and the tolower() function is in a loop to convert each input string character to lowercase. The converted string is then printed to the console using printf().

Note that the tolower() function only works with characters, so you need to loop through each character of the string and apply the function to each character individually in order to convert the entire string to lowercase.

Example 4: Comparing characters case-insensitively 


 
#include <stdio.h>
#include <ctype.h>
int main() {
char ch1 = 'X';
char ch2 = 'x';
if (tolower(ch1) == tolower(ch2)) {
printf("The characters are equal (case-insensitive).\n");
} else {
printf("The characters are not equal (case-insensitive).\n");
}
return 0;
}
Copy code

Output:

In this example, the tolower() function converts ch1 and ch2 to lowercase before comparing them. This allows us to compare the characters case-insensitively, since the uppercase and lowercase versions of the same letter are treated as equal. The if statement tests whether the two characters are equal (case-insensitive), and prints the appropriate message to the screen. 

Example 5: Converting characters in an array to lowercase 


 
#include <stdio.h>
#include <ctype.h>
int main() {
char characters[] = {'A', 'b', 'C', 'd', 'E', 'f'};
int length = sizeof(characters) / sizeof(char); // calculate the length of the array
// convert each character to lowercase
for(int i = 0; i < length; i++) {
characters[i] = tolower(characters[i]);
}
// print the lowercase characters
for(int i = 0; i < length; i++) {
printf("%c ", characters[i]);
}
return 0;
}
Copy code

Output:

a b c d e f

In this code, an array of characters is defined, and the length of the array is calculated using the sizeof() operator. The tolower() function is in a loop to convert each character in the array to lowercase. The converted characters are then printed to the console using printf().

Endnotes

In conclusion, the function tolower() is a useful and uncomplicated resource that can enhance the efficiency and effectiveness of C programming. This article outlines the fundamentals of tolower() and gives various instances of how you can apply in C programs. To learn more, check out C programming courses.

Read More

Explore toupper() Function in C with Examples
Explore toupper() Function in C with Examples
The toupper() function takes a single character as input and returns the corresponding uppercase character. It is commonly used in conjunction with string manipulation functions to convert entire strings to...read more
Understanding Strchr() Function in C
Understanding Strchr() Function in C
This blog explains the strchr() Function in C programming with the help of examples. Letโ€™s understand!This blog explains the strchr() Function in C programming with the help of examples. Letโ€™s...read more

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio