C - 比较数字字符串
出于专业的好奇心,在 C 中比较两个全数字字符串的最安全/最快/最有效的方法是什么?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char str1[5] = "123";
char str2[5] = "123";
char *ptr;
if(atoi(str1) == atoi(str2))
printf("Equal strings");
if(strtol(str1,&ptr,10) == strtol(str2,&ptr,10))
printf("Equal strings");
if(strcmp(str1,str2)==0)
printf("Equal strings");
return 0;
}
Out of professional curiosity, what is the safest / fastest / most efficient way to compare two fully numeric strings in C?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char str1[5] = "123";
char str2[5] = "123";
char *ptr;
if(atoi(str1) == atoi(str2))
printf("Equal strings");
if(strtol(str1,&ptr,10) == strtol(str2,&ptr,10))
printf("Equal strings");
if(strcmp(str1,str2)==0)
printf("Equal strings");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
strcmp ()
在我看来,因为它不需要任何数字转换。但在这种情况下,您需要确保其中之一存储仅包含数字字符的字符串。您还可以对字符串
EDIT1
执行
memcmp ()
正如其他人关于前导零所指出的那样,您可以手动扫描前导零并调用strcmp ( )
或memcmp ()
通过将指针传递给第一个非零数字。EDIT2
下面的代码告诉了我想说的。这仅适用于整数,不适用于浮点数。
对于浮点数,应手动删除小数点后的尾随零。
或者手动完成所有工作。
EDIT4
我还希望您看看这段浮点代码。这将检测小数点之前的前导零和小数点之后的尾随零。例如,对于
以下代码,
00000000000001.10000000000000
和1.1
将是Equal
使用前需要进行一些测试。
strcmp ()
in my opinion, as it does not need any numeric conversions. But in this case you need to make sure that one of them stores a string which contains only numeric characters.Also you can do
memcmp ()
on the stringEDIT1
As pointed out by others about the leading zeros, you can manually scan through the leading zeros and call
strcmp ()
ormemcmp ()
by passing a pointer to the first non-zero digit.EDIT2
The below code tells what i am trying to say. This is only for integers, not for floating point numbers.
For floating point numbers, the trailing zeros after the decimal point should be chopped out manually.
Or do the whole stuff manually.
EDIT4
I would also like you to have a look at this code for floating point. This will detect leading zeros before the decimal and trailing zeros after the decimal. For example
00000000000001.10000000000000
and1.1
will beEqual
for the below codeNeeds some testing before use.
str(n)cmp
是最快、最安全的。str(n)cmp
is the fastest and safest.假设您希望它们相同,
strncmp
将是最快、最安全的,因为它可以进行直接比较,无需任何转换。它通常也被认为比strcmp
更安全。但是,如果您希望
00
和0
相等,或者以其他方式稍微不同地表示相同的数字,则需要使用atoi.
Assuming you are looking for them to be idential,
strncmp
will be the fastest and safest since it can do a direct comparison without any conversions. It is also generally considered safer thanstrcmp
.However, if you want
00
and0
to be equal, or other ways that you can represent the same number slightly differently, you will need to useatoi
.在我看来,“最安全”的方法可能是将两个参数都转换为整数然后进行测试,这样您就可以避免潜在的前导零问题。不过,这可能不是最快或最有效的方法。
In my opinion, the "safest" way would likely be to convert both arguments to integers and then test, as that way you'll avoid the potential leading-zeros problem. It's probably not the fastest or most efficient method, though.
您可以简单地使用以下内容:
if(strcmp("123","123") == 0)
{
}
else
{
}
在我看来它应该有效。
You can simply use following :
if(strcmp("123","123") == 0)
{
}
else
{
}
In my opinion it should work.
我建议对整数采用这种方式:
I suggest this way for integers: