The tolower function C programming converts an uppercase letter to a corresponding lowercase letter. In the default “C” locale, an uppercase letter is any of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
, which translates respectively to: a b c d e f g h i j k l m n o p q r s t u v w x y z
.
One thing you should remember is that before using the tolower, you have to include ctype.h
it because it is declared in ctype.h
. The tolower function takes one argument in the form of an integer and returns the value of type int.
Syntax of tolower function C:
//Syntax of tolower int tolower(int c);
Parameters:
c
=> character to classify
Return value:
The lowercase version of c
or unmodified c
if no lowercase version is listed in the current “C” locale.
Note:
In other locales, if an uppercase character has more than one correspondent lowercase character, this function always returns the same character for the same value of c; otherwise, the argument is returned unchanged
C program to understand the working of tolower function:
Consider the below code where I am passing different characters in the tolower function. You can see the output of this function with different characters.
#include <stdio.h> #include <ctype.h> int main() { int c = 'A'; int ret = tolower((unsigned char)c); printf("tolower(%c) = %c\n", c, ret); c = 'Z'; ret = tolower((unsigned char)c); printf("tolower(%c) = %c\n", c, ret); c = 'a'; ret = tolower((unsigned char)c); printf("tolower(%c) = %c\n", c, ret); c = '*'; ret = tolower((unsigned char)c); printf("tolower(%c) = %c\n", c, ret); return 0; }
Output:
tolower(A) = a tolower(Z) = z tolower(a) = a tolower(*) = *
C Program To Convert Uppercase String To Lowercase Using the tolower:
Algorithm:
1.
Traverse the given string character by character and passed it into thetolower
function.
2.
The tolower function converts the uppercase letter to a corresponding lowercase letter and left another letter unchanged.
3.
Using the putchar function we will print the return value of the tolower function.
#include <stdio.h> #include <ctype.h> int main () { unsigned int i=0; char str[]="AtiClE WorlD.COM"; while (str[i]) { putchar (tolower((unsigned char)str[i])); i++; } return 0; }
Output:
aticle world.com
Note:
If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of tolower is undefined.
As we know the behavior of tolower is undefined if the argument’s value is neither representable as unsigned char nor equal to EOF. So to use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char. Because it is good to convert signed char to unsigned char before being assigned or converted to a larger signed type.
int my_tolower(char ch) { return tolower((unsigned char)ch); }
Recommended Post:
- How to use the islower function in C?
- Use of iscntrl function in C.
- How to use isalpha function in C programming?
- Use isalnum function in C programming?
- How to use isdigit function in C programming?
- How to use sizeof operator in C.
- _Alignof or alignof Operator in C
- Alignment specifiers in C ( _Alignas).
- Function Specifiers in C.
- Type qualifiers in C.
- Punctuators in C.
- Elements of C language.
- C String Literals with Its Types
- C identifiers and naming rules.