我正在获取用户输入,并且我想确定用户是否输入了字母、整数或运算符。我可以使用 sscanf 成功确定它是否是一个整数,但我很难确定它是否是一个字母。
通过字母,我的意思是:AZ,az。
int main(){
char buffer[20];
int integer;
printf("Enter expression: ");
while (fgets(buffer, sizeof(buffer), stdin) != NULL){
char *p = strchr(buffer, '\n'); //take care of the new line from fgets
if (p) *p = 0;
//Buffer will either be a integer, an operator, or a variable (letter).
//I would like a way to check if it is a letter
//I am aware of isalpha() but that requires a char and buffer is a string
//Here is how I am checking if it is an integer
if (sscanf(buffer, "%d", &integer) != 0){
printf("Got an integer\n");
}
else if (check if letter)
// need help figuring this out
}
else{
// must be an operator
}
}
}