当前位置: C语言 -- 标准库 -- <ctype.h> -- isalpha

isalpha函数


概要:
#include <ctype.h>
int isalpha(int c);

描述:

该函数用于检查字符是否为字母。

该函数行为会受当前语言环境影响。默认环境中(即“C”语言环境),字母为以下成员之一:

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

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


字母字符需满足以下条件之一:

① islower函数或者isupper函数返回true的字符。

② 本地字符集中特定字母字符,并且该字符不满足iscntrl函数、isdigit函数、ispunct函数或者isspace函数返回true的条件。

(注:对于额外字符,islower函数、isupper函数返回值可能是true,也可能是false;四种组合都有可能。)


默认环境中(即“C”语言环境),isalpha函数仅对islower函数或者isupper函数返回true的字符返回true。


参数:
int c

参数c为int类型整数,其值可用unsigned char类型表示或者等于宏EOF。如果参数c是其它值,函数行为是未定义的。


返回值:

如果参数c是字母,函数返回非0值(即true);如果参数c不是字母,函数返回0(即false)。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
/*函数isalpha范例*/

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

int main(void)
{
    int i = 0;
    int count = 0;    //计数器,统计字母数量。
    const char str[] = "I am a slow walker, but I never walk backwards.";

    while(str[i])
    {
        if(isalpha(str[i]))
            ++count;
        ++i;
    }

    printf("There are %d alphabets in the sentence.", count);

    return 0;
}

输出:

There are 36 alphabets in the sentence.


相关内容:
isupper 检查字符是否为大写字母的函数。
islower 检查字符是否为小写字母的函数。
isalnum 检查字符是否为字母或者十进制数字字符的函数。


主要参考资料:

1、ISO/IEC 9899:2024

2、ISO/IEC 9899:2018