C program to check if a character is lowercase using islower

C program to check if a character is lowercase using islower:

islower method can be used to check if a character is lowercase or not. This method is defined in the ctype.h header file and we can use it to quickly check if a character is lowercase or not in C. If the character is a lowercase alphabet, (a-z), it returns a nonzero value. Else, it returns 0.

In this post, we will learn how to use this method with examples.

Definition of islower:

The islower method is defined as like below:

int islower(int c)

It takes the character as the parameter and returns an integer value. The character is converted to integer i.e. it converts the character to its ASCII value.

Header file to include:

To use islower, we need to include ctype.h header file in the C program.

Example of islower with an array of characters:

Let’s use islower with an array of characters:

#include <stdio.h>
#include <ctype.h>

int main()
{
    char charArray[] = {'3', 'a', 'b', 'C', 'd', '&', '-'};

    for (int i = 0; i < 7; i++)
    {
        printf("islower(%c) => %d\n", charArray[i], islower(charArray[i]));
    }
    return 0;
}

It will print:

islower(3) => 0
islower(a) => 1
islower(b) => 1
islower(C) => 0
islower(d) => 1
islower(&) => 0
islower(-) => 0

As you can see here, it prints 1 only for lowercase characters. For uppercase characters, it prints 0.

Example of islower with if-else statements:

We can use it with conditional statements like if-else because it returns either a non-zero value of zero. If we check the value with if, it treat the non-zero value as true and zero as false.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char charArray[] = {'3', 'a', 'b', 'C', 'd', '&', '-'};

    for (int i = 0; i < 7; i++)
    {
        if (islower(charArray[i]))
        {
            printf("'%c' is a lowercase character\n", charArray[i]);
        }
    }
    return 0;
}

This program uses the same array we used in the previous example. The if condition prints only the lowercase characters.

If you run this program, it will print the below output:

'a' is a lowercase character
'b' is a lowercase character
'd' is a lowercase character

The if block takes only non-zero numbers as true. So, it prints only the characters for which the return value of islower is non-zero.

Example of islower with user-input characters:

Let’s write a program that takes a character as the input and prints if it is a lowercase or uppercase character:

#include <stdio.h>
#include <ctype.h>

int main()
{
    char c;

    printf("Enter a character: ");
    scanf("%c", &c);

    if (islower(c))
    {
        printf("%c is in lowercase\n",c);
    }
    else
    {
        printf("%c is in uppercase\n",c);
    }
    return 0;
}

It reads a character from the user, stores it in the character variable c and uses islower to check if the character is lowercase or uppercase. Based on its return value, it prints a message.

Enter a character: c
c is in lowercase

Enter a character: C
C is in uppercase

Enter a character: X
X is in uppercase

C islower example

You might also like: