c - 如何检查 C 中的某些字符

标签 c

我正在编写一个脚本,我需要确保它们只输入某些字符。其中包括“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/

相关文章:

c - 如何在 Linux pthreads 中设置线程的名称?

c - Visual Studio 2017 不支持C11 新特性_Generic

c - void 函数(不打印)- 使用递归反转字符串

c - 需要帮助修复此 C Socket ICMP 程序

c - 为什么 C 没有二进制文字?

c - 将指向字符的指针传递给函数

c - getenv ("APPDATA") 如果当前用户的用户名包含 unicode 字符,则返回不正确的值

c++ - 如何强制执行额外的预处理器宏扫描

通过命名管道实现的客户端-服务器多线程程序

c - 8字节内存可以输入4字节吗?没有损失?