The islower function in C programming checks whether the argument passed is a lowercase letter or not. In the “C” locale lowercase letters are 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.
It is declared in ctype.h
and takes one argument in the form of integer and returns the value of type int. If islower
returns a nonzero value, it is guaranteed that iscntrl, isdigit, ispunct, and isspace return zero for the same character in the same locale.
Syntax of islower function in C:
//Syntax of islower int islower(int c);
Parameters:
c
=> character to classify
Return value:
Non-zero value
=> If the argument is a lowercase letter.
0
=> If the argument is neither a lowercase letter.
Example,
Input : a Output : Non-zero value Input : A Output : zero Input : 1 Output : Zero Input : @ Output : Zero Input : b Output : Non-zero value
C Program to Check whether a Character Entered by User is a lower-case letter or not Using the islower:
Let’s see a C program to check given character is a lowercase letter or not.
#include <stdio.h> #include <ctype.h> int main() { int c; printf("Enter a Valid Character: "); c = fgetc(stdin); if (islower(c) == 0) { printf("%c is not a lowercase letter.", c); } else { printf("%c is a lowercase letter.", c); } return 0; }
Output:
Output1:
Enter a character: a
a
is a lowercase letter.
Output2:
Enter a character: A
A
is not a lowercase letter.
Output3:
Enter a character: @
@
is not a lowercase letter.
Print Count of the lowercase letter in given input string:
There are many applications of islower in C programming. But to find out the count of lowercase characters in a given input stream is very popular. So let’s see a C code to find the count of the lowercase characters in the given input stream.
Algorithm:
1.
Traverse the given string character by character and passed it into the islower function.
2.
Increment the counter variable whenever encounter the lowercase letter.
3.
Break the loop when encountered the null character (Limitation there must not be another null character in the string except the terminating null character).
4.
Return the value of the counter from the function and print the value in the main function.
#include <ctype.h> #include<string.h> #include <stdio.h> unsigned int findCountLowerCaseLetterGivenStream(char *str) { unsigned int counter = 0; if (str != NULL) { unsigned int i = 0; // counting of control char while (str[i] != '\0') { if (islower((unsigned char)str[i])) { ++counter; } ++i; } } // returning total number of lowercase char // present in given input stream return (counter); } int main() { char str[] = "AticleWorld"; unsigned int counter = findCountLowerCaseLetterGivenStream(str); printf("Total number of char in input stream is : %u\n\n", strlen(str)); printf("\nNumber of lowercase char in the " "given input stream is : %u\n\n", counter); return 0; }
Output:
Total number of char in input stream is : 11
Number of lowercase char in the given input stream is : 9
Print the string till the first lowercase letter is encountered:
Another popular program is to print given a string till the first lowercase character is encountered. With the help of islower() we can easily do it. So let’s see a small program.
Algorithm:
1.
Traverse the given string character by character and passed it into the islower function.
2.
If the character is not a lowercase character it returns zero. You can see in the while loop braces, I have used logical Not ( ! ) with islower(). So for each non-lowercase letter loop will run and print the character on stdout.
3.
When the lowercase character will encounter the while loop will break.
#include <ctype.h> #include<string.h> #include <stdio.h> int main() { char str[] = "ATICLE_C_TUTORIALworld"; unsigned int i = 0; // printing till first lowercase char while (!islower((unsigned char)str[i])) { putchar(str[i]); ++i; } return 0; }
Output:
ATICLE_C_TUTORIAL
First lowercase letter in a string (Iterative and Recursive):
It is another interesting application of an islower function to find the first occurrence of the lowercase letter in a given input string. We will see it using both ways iterative and recursive.
Example,
Input : ATICLEworld Output : w Input : ABCDa Output : a
Iterative Way:
Using the linear search we will find the first lowercase character with the help of islower()
.
#include <ctype.h> #include<string.h> #include <stdio.h> char firstLoweCaseCharGivenStream(char *str) { unsigned int i = 0; char firstLowerCaseChar = 0; if (str != NULL) { // find first lowercase char while (str[i] != '\0') { if (islower((unsigned char)str[i])) { firstLowerCaseChar = str[i]; break; } ++i; } } // returning first lowercase char // present in given input stream return (firstLowerCaseChar); } int main() { //String must have only one null char (terminating null) char str[] = "ATICLEworld.com"; unsigned int firstLowerCaseChar = firstLoweCaseCharGivenStream(str); if(firstLowerCaseChar != 0) { printf("Total number of char in input stream is : %u\n\n", strlen(str)); printf("\nFirst lower case char in the " "given input stream is : %c \n\n", firstLowerCaseChar); } return 0; }
Output:
Recursive Way:
Recursively traverse the string and if any lowercase is found return that character
#include <ctype.h> #include<string.h> #include <stdio.h> char firstLoweCaseCharGivenStream(char * str, int i) { if (str != NULL) { if (str[i] == '\0') { return 0; } if (islower(str[i])) { return str[i]; } } return firstLoweCaseCharGivenStream(str, i+1); } int main() { //String must have only one null char (terminating null) char str[] = "ATICLEworld.com"; unsigned int firstLowerCaseChar = firstLoweCaseCharGivenStream(str, 0); if(firstLowerCaseChar != 0) { printf("Total number of char in input stream is : %u\n\n", strlen(str)); printf("\nFirst lower case char in the " "given input stream is : %c \n\n", firstLowerCaseChar); } return 0; }
Output:
Note:
If the argument’s value (c) is neither representable as unsigned char not equal to EOF, the behavior of islower is undefined.
As we know the behavior of islower 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_islower(char ch) { return islower((unsigned char)ch); }
Recommended Post:
- 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.
- Stringizing operator (#) in C.