简体   繁体   English

strncmp 不能正常工作

[英]strncmp don't work as it should

I'm having problems using strncmp .我在使用strncmp遇到问题。 As I read, theorically strncmp should return 0 if the characters compared of two strings are equal;正如我所读到的,如果两个字符串的比较字符相等,理论上strncmp应该返回 0; however, when I do the comparation, the code misbehaves and makes a false positive (Not being equal the characters, still makes the if clause).但是,当我进行比较时,代码行为不端并产生误报(不等于字符,仍然使 if 子句)。 Here the code:这里的代码:

#include <stdio.h>
#include <string.h>

int main(){
    char *frase1="some string";
    char *frase2="another string";
    char *frase3="some other string";

//Comparar frases desde inicio
    if(strncmp(frase1, frase2, 200))printf("1<->2, 200 characters\n");
    if(strncmp(frase1, frase3, 20))printf("1<->3, 20 characters\n");
    if(strncmp(frase1, frase3, 4))printf("1<->3, 4 characteres\n");

    return 0;
}

If the strings are equal (At least the compared characters), they should print the message;如果字符串相等(至少是比较的字符),它们应该打印消息; if not, do nothing;如果没有,什么都不做; so I still don't understand why the first Condition becomes true.所以我仍然不明白为什么第一个条件变为真。

Any ideas?有任何想法吗?

strcmp & strncmp functions return 0 if strings are equal.如果字符串相等, strcmpstrncmp函数返回0 You should do:你应该做:

if (strncmp(frase1, frase3, 4) == 0) ...

ie: IE:

char *str1 = "Example 1";
char *str2 = "Example 2";
char *str3 = "Some string";
char *str4 = "Example 1";

if (strncmp(str1, str2, 7) == 0) printf("YES\n");  // "Example" <-> "Example"
else printf("NO\n");

if (strncmp(str1, str3, 2) == 0) printf("YES\n");  // "Ex" <-> "So"
else printf("NO\n");

if (strcmp(str1, str4) == 0) printf("YES\n");      // "Example 1" <-> "Example 2"
else printf("NO\n");

yields YES , NO , YES .产生YES , NO , YES

If the strings are equal (At least the compared characters), they should print the message如果字符串相等(至少是比较的字符),它们应该打印消息

No, if the strings are equal they should NOT print the message since strncmp returns 0 on equality.不,如果字符串相等,则不应打印消息,因为strncmp在相等时返回0 Since the last check returns 0 it doesn't get printed while the other two are non-zero ie unequal thus you see them getting printed.由于最后一次检查返回0因此不会打印,而其他两个非零即不相等,因此您会看到它们被打印。

strncmp 's documentation should give you more clarity. strncmp的文档应该让你更清楚。 It returns它返回

  • 0 when lhs == rhs 0 当 lhs == rhs
  • < 0 when lhs < rhs < 0 当 lhs < rhs
  • > 0 when lhs > rhs > 0 当 lhs > rhs

The strncmp() function returns zero if the strings are equal, and a non-zero value otherwise.如果字符串相等,则 strncmp() 函数返回零,否则返回非零值。 So if you compare two strings that are not similar you get a non-zero value, and non-zero values are TRUE as far as if statements are concerned.因此,如果您比较两个不相似的字符串,则会得到一个非零值,并且就 if 语句而言,非零值是 TRUE。 So what you need to do is:所以你需要做的是:

   if(!strncmp(frase1, frase2, 200))printf("Mismo insulto no creo\n");

you should make the strncmp() == 0 as the expression to be evaluated您应该将strncmp() == 0作为要评估的表达式

working code:工作代码:

#include <stdio.h>
#include <string.h>

int main(){
char *frase1="some string";
char *frase2="another string";
char *frase3="some other string";

//Comparar frases desde inicio
if(strncmp(frase1, frase2, 200) == 0)
   printf("1<->2, 200 characters\n");
if(strncmp(frase1, frase3, 20) == 0)
   printf("1<->3, 20 characters\n");
if(strncmp(frase1, frase3, 4) == 0)
   printf("1<->3, 4 characteres\n");

return 0;
}

One, it's considered good practice to keep strings and identifiers in English when posting example code.第一,在发布示例代码时将字符串和标识符保持为英文被认为是一种很好的做法。 Readability trumps fun.可读性胜过乐趣。

Two, if blocks get executed if the condition is true .第二, if条件为trueif执行if块。 A value of 0 is considered false , while any value other than 0 is considered true .的值0被认为是false ,而以外的值0被认为是true

Ie, your problem is not strncmp() , but a confusion about true and false.即,您的问题不是strncmp() ,而是对真假的混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM