首页 > 编程笔记

C语言islower():判断一个字符是否为小写英文字母

islower() 函数是 C 语言的标准库函数,定义在<ctype.h>头文件中。

islower() 函数用于检查给定的字符是否为小写英文字母(a~z,对应的 ASCII 码值为 97~122)。

islower() 函数的原型如下:

int islower(int c);

参数

c 表示要检查的字符。虽然参数是 int 类型,但通常传入的是 char 类型的字符。

返回值

如果传入的字符是小写字母,则返回非零值;如果传入的字符不是小写字母,则返回零。

【实例】用 islower() 函数检查一个字符串中的每个字符是否是小写字母,请看下面的 C语言代码。
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[] = "Hello World!";

    for(int i = 0; str[i] != '\0'; i++) {
        if(islower(str[i])) {
            printf("Character '%c' at index %d is a lowercase letter.\n", str[i], i);
        } else {
            printf("Character '%c' at index %d is NOT a lowercase letter.\n", str[i], i);
        }
    }

    return 0;
}
输出结果为:

Character 'H' at index 0 is NOT a lowercase letter.
Character 'e' at index 1 is a lowercase letter.
Character 'l' at index 2 is a lowercase letter.
Character 'l' at index 3 is a lowercase letter.
Character 'o' at index 4 is a lowercase letter.
Character ' ' at index 5 is NOT a lowercase letter.
Character 'W' at index 6 is NOT a lowercase letter.
Character 'o' at index 7 is a lowercase letter.
Character 'r' at index 8 is a lowercase letter.
Character 'l' at index 9 is a lowercase letter.
Character 'd' at index 10 is a lowercase letter.
Character '!' at index 11 is NOT a lowercase letter.

示例中用 islower() 函数检查每个字符是否为小写字母,在这个特定的字符串中,小写的字母 "e", "l", "o", "r", "l", "d" 都被正确地识别为小写字母。

推荐阅读