Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
communitycreator

What is the tolower() function in C?

Daki Dinesh

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In C, the tolower() function is used to convert uppercase letters to lowercase.

When an uppercase letter is passed into the tolower() function, it converts it into lowercase.

However, when a lowercase letter is passed into the tolower() function, it returns the same letter.

Note: In order to use this function, ctype.h needs to be included.

Syntax

The function takes a letter as a parameter and returns it in lowercase.

The prototype of the tolower() function is:

int tolower(int argument);

Note: The character is passed in and returned as int, i.e., the ASCII value of the character moves around.

Code

Let’s start with the most basic example of converting an uppercase letter into lowercase using this function.

Take a look at the code below:

#include<stdio.h>
#include <ctype.h>
int main()
{
char ch;
// letter to convert to lowercase
ch = 'B';
printf("%c in lowercase is represented as %c",
ch, tolower(ch));
return 0;
}

Now, let’s look at how to convert words and sentences to lowercase. However, we can’t just give all the words as input because tolower() can only take one character at a time.

So, to do this, we need to use a loop over the word to individually convert each letter to lowercase.

#include<stdio.h>
#include <ctype.h>
int main()
{
// counter for the loop
int i = 0;
// word to convert to uppercase
char word[] = "eDuCaTiVE.Io\n";
char chr;
// Loop
while (word[i]) {
chr = word[i];
printf("%c", tolower(chr));
i++;
}
return 0;
}

In the code above, we pass each letter of the word to the tolower() function to get and display its lowercase form.

RELATED TAGS

c
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring