<ctype.h>
头文件中,用于检查传入的字符是否是字母('A'~'Z' 和 'a'~'z')或数字(0~9)。int isalnum(int c);
#include <stdio.h> #include <ctype.h> int main() { char ch; printf("input a character:"); /*输入一个字符*/ scanf("%c", &ch); if (isalnum(ch)) { /*判断输入字符是否是字母或数字字符*/ printf("%c is alnum.", ch); } else { printf("%c is not alnmu.", ch); } }运行结果:
input a character:.
. is not alnmu.
#include <stdio.h> #include <ctype.h> int main() { char str[] = "C0ding is fun!"; for(int i = 0; str[i] != '\0'; i++) { if(isalnum(str[i])) { printf("'%c' is an alphanumeric character.\n", str[i]); } else { printf("'%c' is NOT an alphanumeric character.\n", str[i]); } } return 0; }运行结果:
'C' is an alphanumeric character.
'0' is an alphanumeric character.
'd' is an alphanumeric character.
'i' is an alphanumeric character.
'n' is an alphanumeric character.
'g' is an alphanumeric character.
' ' is NOT an alphanumeric character.
'i' is an alphanumeric character.
's' is an alphanumeric character.
' ' is NOT an alphanumeric character.
'f' is an alphanumeric character.
'u' is an alphanumeric character.
'n' is an alphanumeric character.
'!' is NOT an alphanumeric character.
本文链接:http://task.lmcjl.com/news/19108.html