- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 C 代码,其中我在 ctype.h 中使用标准库函数 isalpha(),这是在 Visual Studio 2010-Windows 上。在下面的代码中,如果 char c 是 '£',则 isalpha 调用返回一个断言,如下面的快照所示:
char c='£';
if(isalpha(c))
{
printf ("character %c is alphabetic\n",c);
}
else
{
printf ("character %c is NOT alphabetic\n",c);
}
我可以看出这可能是因为 8 位 ASCII 没有这个字符。
那么我该如何处理 ASCII 表之外的此类非 ASCII 字符呢?
我想做的是,如果找到任何非字母字符(即使它包含不在 8 位 ASCII 表中的此类字符),我希望能够忽略它。
最佳答案
您可能希望转换发送到 isalpha
的值(以及在 <ctype.h>
中声明的其他函数)到 unsigned char
isalpha((unsigned char)value)
这是在 C 语言中适合强制转换的(并非如此)少数情况之一。
编辑以添加解释。
根据 the standard , 重点是我的
7.4
1 The header
<ctype.h>
declares several functions useful for classifying and mapping characters. In all cases the argument is anint
, the value of which shall be representable as anunsigned char
or shall equal the value of the macroEOF
. If the argument has any other value, the behavior is undefined.
投向unsigned char
确保调用 isalpha()
不会调用未定义的行为。
关于c - isalpha() 给出断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6693654/
这个问题在这里已经有了答案: C++: Why does space always terminate a string when read? (4 个答案) 关闭 2 年前。 我编写了一个代码,以
我有GLFW key_callback() 并且我正在尝试检测是否按下了字母字符。我用这个功能isalpha() .我注意到如果执行以下代码,某些键(例如 Shift 和 Alt)会被视为字母字符:
#include #include #include #include #include int main() { char userPassword[20]; printf
正在解决一些 C 问题,但遇到了困难。我可以想出另外一两种解决方法,但我想更好地理解为什么我的检查失败了。 #include #include #include #include int ma
我正在尝试使用 for 循环逐个字符地遍历两个字符串,看看它们在哪里相等,在哪里不同,如果是的话,如果两个都是字母,那么我会做一个条件,但如果一个是字母和一个是下划线,然后我做别的事情。循环的第一部分
我从事此工作已经有一段时间了,现有的答案几乎没有帮助。我是编程新手,正在尝试编写我的程序的一个子部分,它试图检查任何给定的输入是否仅由字母组成。 为此,我想到的想法是通过使用一次传递每个字符的循环将整
我有一个 C 代码,其中我在 ctype.h 中使用标准库函数 isalpha(),这是在 Visual Studio 2010-Windows 上。在下面的代码中,如果 char c 是 '£',则
我有一个程序,它使用 isalpha 比较字符串中的每个元素。我输入一个字符串,它调用函数来循环遍历数组并检查所有字符是否都是字母。如果是,则打印 true,否则打印 false。我的问题是我似乎无法
只是为了好玩,我编写了 isalpha 的这个实现,它可以工作,但我有很多警告,你能告诉我如何修复它们,或者我怎样才能以更好的方式做到这一点? 警告: 第6行:指针和整数的比较 第 23 行:传递“指
我正在用 C 语言编写一个程序,其中涉及选择 4 个选项。但是,我无法验证变量 Option 的值是否是 1、2、3 或 4 之外的字母或数字。当我输入字母时,它继续循环 print 语句,但不循环输
我正在尝试使用 isaplha() 函数来检查每个字符串的每个字符,以确定它是否是字母。但由于某种原因它不起作用。尽管 if 语句有参数,程序总是进入 printf() 函数。看起来代码一切正常: #
我正在对我的 C 程序进行一些错误检查,目前正在测试以确保输入数据点是 float/int (本质上不是字符) 为此,我使用了 isalpha 函数。它适用于 100 以下的所有数字,但如果我输入 1
我正在尝试检查给定字符串中的每个字符是否都是字母。如果不是,则用空格替换该字符。到目前为止我所拥有的是: def removePunctuation(txt): for char in txt
有没有办法让 python isalpha 方法理解 scandics?我尝试了以下方法: >>> import locale >>> locale.getlocale() (None, None)
我一直在尝试使用 JavaScript 验证表单的内容。我需要检查“名称”字段是否仅包含字符和空格。如果存在任何其他字符,我想显示警报。这在 C 和 Java 中很容易做到,但我似乎无法在 JavaS
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在尝试检测使用了哪些数学运算符,然后将该运算符分配给一个变量。这是我的代码: start = "3x - 2 = 11" op = "+" for m in start: if m.isalp
while(1) { char buff[1000]; printf("Enter the word: "); fgets(buff, 1000
我正在尝试使用 isalpha() 来验证输入并确保它不是字母。我已尝试正确使用它,但我想我可能遗漏了一些东西。 有问题的部分: scanf("%i", &entry); while (entry !
我正在使用 isalpha() 来确定该行是否有字符,它显示“90 1 0”行包含字母字符。 这里是相关代码 bool digitTest(char *test, int arysize) { int
我是一名优秀的程序员,十分优秀!