在 C 编程中,isalpha()
函数用于检查一个字符是否是字母(a 到 z 和 A 到 Z)。
如果传递给 isalpha()
的字符是字母,它将返回一个非零整数;如果不是,它将返回 0。
isalpha()
函数定义在 <ctype.h> 头文件中。
C isalpha() 原型
int isalpha(int argument);
函数 isalpha()
接受一个整数作为单个参数,并返回一个整数值。
即使 isalpha()
接受整数作为参数,字符也会被传递给 isalpha()
函数。
当字符被传递时,它在内部会被转换为其 ASCII 值对应的整数值。
isalpha()返回值
返回值 | 备注 |
---|---|
零 (0) | 如果参数不是字母。 |
非零数字 | 如果参数是字母。 |
示例:C isalpha() 函数
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
c = 'Q';
printf("\nResult when uppercase alphabet is passed: %d", isalpha(c));
c = 'q';
printf("\nResult when lowercase alphabet is passed: %d", isalpha(c));
c='+';
printf("\nResult when non-alphabetic character is passed: %d", isalpha(c));
return 0;
}
输出
Result when uppercase alphabet is passed: 1024 Result when lowercase alphabet is passed: 1024 Result when non-alphabetic character is passed: 0
注意: 当字母字符传递给 isalpha()
时,您在您的系统上可能会得到一个不同的非零整数。但是,当您将非字母字符传递给 isalpha()
时,它总是返回 0。
示例:C 程序检查用户输入的字符是否为字母
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
if (isalpha(c) == 0)
printf("%c is not an alphabet.", c);
else
printf("%c is an alphabet.", c);
return 0;
}
输出
Enter a character: 5 5 is not an alphabet.