检查字符类型

基本类型 / 字符类型

char c1 = 'A';
bool isLetter = isalpha((unsigned char)c1);
bool isNumber = isdigit((unsigned char)c1);
//isLetter is true
//isNumber is false

char c2 = '$';
bool isPunct = ispunct((unsigned char)c2);
//isPunct is true

const char *symbolList = "@#$%^&*";
bool isInList = c2 != '\0' && strchr(symbolList, c2) != nullptr;
//isInList is true
//strchr also finds '\0', the end of the string, hence the check

printf("isLetter is %s\n", isLetter ? "true" : "false");
printf("isNumber is %s\n", isNumber ? "true" : "false");
printf("isPunct is %s\n", isPunct ? "true" : "false");
printf("isInList is %s\n", isInList ? "true" : "false");