我正在编写一个脚本,我需要确保它们只输入某些字符。其中包括“x”、“/”、“+”、“-”、“%”(基本数学运算符)、-z 中的每个字母和每个数字。我在下面有以下内容,仅检查字母和数字。如何检查是否只使用了某个特定的,而其他所有内容(例如“&”或“>”)是否正确处理了错误?
//check to see if user has input an incorrect symbol or letter
if (isalpha(symbol) || isalnum(symbol))
{
printf("You must enter a math operator, not a letter or number. \n \n");
}
else {//move along nothing to see here
}
最佳答案
用所有允许的字符创建一个字符串,然后检查该字符串。
char* ok = "/+-*%";
if (isalpha(symbol) || isalnum(symbol) || strchr(ok, symbol) == NULL)
{
printf("You must enter a math operator, not a letter or number. \n \n");
}
else {//move along nothing to see here
}
关于c - 如何检查 C 中的某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1479850/