isalpha() 函数在 ctype.h 头文件中定义。它用于检查指定的字符是否为字母(a 到 z 和 A-Z)。
int isalpha(int argument); #where argument will be a character
isalpha() 函数接受一个整数形式的单个参数,并返回一个整数值。当传入一个字符时,它会被转换为对应的 ASCII 值。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 参数 | 要检查的字符 | 必需 |
如果给定字符是字母,isalpha() 返回非零整数;否则返回零。当传入字母字符时,我们会得到一个不同的非零整数。
| 输入 | 返回值 |
|---|---|
| 零 | 如果参数不是字母 |
| 非零数字 | 如果参数是字母 |
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
ch = 'A';
printf("\nIf uppercase alphabet is passed: %d", isalpha(ch));
ch = 'a';
printf("\nIf lowercase alphabet is passed: %d", isalpha(ch));
ch='+';
printf("\nIf non-alphabetic character is passed: %d", isalpha(ch));
return 0;
}
输出
If uppercase alphabet is passed: 1 If lowercase alphabet is passed: 2 If non-alphabetic character is passed: 0
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
printf("Please enter a character: ");
scanf("%c", &ch;);
if (isalpha(ch) == 0)
printf("The given %c is not an alphabet.", ch);
else
printf("The given %c is an alphabet.", ch);
return 0;
}
输出
Please enter a character: 10 The given 10 is not an alphabet.