islower() Function

The islower() function in C checks if a character is a lowercase letter. It is commonly used to validate and process text data by determining if a character falls within the range of lowercase alphabetic letters.


Syntax of islower()

</>
Copy
int islower(int c);

Parameters

ParameterDescription
cAn integer representing the character to be checked, typically cast from a char or EOF.

It is noteworthy that what constitutes a lowercase letter may vary by locale. In the default “C” locale, only the letters a through z are considered lowercase. Other locales might have different rules, but they never treat characters that return true for iscntrl(), isdigit(), ispunct() or isspace() as lowercase.

Return Value

The function returns a nonzero value (true) if the character is a lowercase alphabetic letter, and zero (false) otherwise.


Examples for islower()

Example 1: Basic Lowercase Letter Check

This example demonstrates the basic usage of islower() to check if a given character is a lowercase letter.

Program

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

int main() {
    int ch = 'g';

    if (islower(ch)) {
        printf("The character %c is lowercase.\n", ch);
    } else {
        printf("The character %c is not lowercase.\n", ch);
    }

    return 0;
}

Explanation:

  1. A character ch is initialized with the value 'g'.
  2. The islower() function checks if 'g' is a lowercase letter.
  3. Since 'g' is indeed lowercase, the program prints a confirmation message.

Program Output:

The character g is lowercase.

Example 2: Checking Mixed Characters

This example illustrates how islower() can be used to determine the case of various characters in a string.

Program

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

int main() {
    char chars[] = "aZ3#b";
    int i = 0;

    while (chars[i] != '\0') {
        if (islower(chars[i])) {
            printf("'%c' is lowercase.\n", chars[i]);
        } else {
            printf("'%c' is not lowercase.\n", chars[i]);
        }
        i++;
    }

    return 0;
}

Explanation:

  1. A character array "aZ3#b" is defined containing a mix of characters.
  2. The program iterates through each character in the array.
  3. The islower() function checks each character to determine if it is lowercase.
  4. An appropriate message is printed for each character based on the check.

Program Output:

'a' is lowercase.
'Z' is not lowercase.
'3' is not lowercase.
'#' is not lowercase.
'b' is lowercase.

Example 3: Verifying Each Character in a Full String

This example demonstrates how to verify if each character in a complete string is a lowercase letter using islower().

Program

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

int main() {
    char str[] = "hello World";
    int i = 0;

    while (str[i] != '\0') {
        if (islower(str[i])) {
            printf("Character '%c' is lowercase.\n", str[i]);
        } else {
            printf("Character '%c' is not lowercase.\n", str[i]);
        }
        i++;
    }

    return 0;
}

Explanation:

  1. A string "hello World" is defined.
  2. The program iterates over each character in the string.
  3. The islower() function checks whether each character is a lowercase letter.
  4. A message is printed for each character, indicating its lowercase status.

Program Output:

Character 'h' is lowercase.
Character 'e' is lowercase.
Character 'l' is lowercase.
Character 'l' is lowercase.
Character 'o' is lowercase.
Character ' ' is not lowercase.
Character 'W' is not lowercase.
Character 'o' is lowercase.
Character 'r' is lowercase.
Character 'l' is lowercase.
Character 'd' is lowercase.