- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在C中重新实现strcasecmp
函数,我注意到在比较过程中似乎不一致。
来自man strcmp
The strcmp() function compares the two strings s1 and s2. The locale is not taken into account (for a locale-aware comparison, see strcoll(3)). It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
man strcasecmp
The strcasecmp() function performs a byte-by-byte comparison of the strings s1 and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
int strcmp(const char *s1, const char *s2);
int strcasecmp(const char *s1, const char *s2);
#include <stdio.h>
#include <string.h>
int main()
{
// ASCII values
// 'A' = 65
// '_' = 95
// 'a' = 97
printf("%i\n", strcmp("A", "_"));
printf("%i\n", strcmp("a", "_"));
printf("%i\n", strcasecmp("A", "_"));
printf("%i\n", strcasecmp("a", "_"));
return 0;
}
-1 # "A" is less than "_"
1 # "a" is more than "_"
2 # "A" is more than "_" with strcasecmp ???
2 # "a" is more than "_" with strcasecmp
s1
中的当前字符是字母,则无论
s2
中的当前字符是否是字母,它总是会转换为小写。
gcc 9.2.0
。
-fno-builtin
标志进行编译时,我得到的是:
-30
2
2
2
最佳答案
行为是正确的。
每the POSIX str\[n\]casecmp()
specification:
When the
LC_CTYPE
category of the locale being used is from the POSIX locale, these functions shall behave as if the strings had been converted to lowercase and then a byte comparison performed. Otherwise, the results are unspecified.
The POSIX.1-2008 standard says of these functions:
When the LC_CTYPE category of the locale being used is from the POSIX locale, these functions shall behave as if the strings had been converted to lowercase and then a byte comparison performed. Otherwise, the results are unspecified.
strcmp()
一样具有其“自然”结果会破坏排序。
'A' == 'a'
(不区分大小写的比较的定义),那么
'_' > 'A'
和
'_' < 'a'
(ASCII字符集中的“自然”结果)不能同时为true。
关于c - strcasecmp算法有缺陷吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60342445/
我想捕捉发送到我的窗口的按键以存储每个键的状态,并区分键的左右实例,如 WM_LCONTROL 和 WM_RCONTROL. 因为 WM_KEYUP/DOWN 不提供此功能,所以我转向原始输入,我需要
我有以下代码: Area here jQuery: $('#area').on('mouseenter', function(event){ $('#area').addC
这个问题在这里已经有了答案: Why does a RegExp with global flag give wrong results? (7 个答案) 关闭 7 个月前。 我向你保证我没有说谎。
我想知道以下问题的答案。 1) 如果我尝试使用 AVAudioSession 播放音频,而 iphone 扬声器有故障/有缺陷/不工作,会发生什么情况? 2) 有什么方法可以通过程序检查扬声器是正常工
我是一名优秀的程序员,十分优秀!