当前位置: 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

非默认环境中,字母为特定语言环境字母字符集成员,该成员的iscntrlisdigitispunctisspace函数返回值均为false。(:该成员的islowerisupper函数返回值可能是true,也可能是false;四种组合都有可能。)


参数:
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 检查字符是否为字母或者十进制数字字符的函数。