[Solved] strncmp don’t work as it should


strcmp & strncmp functions return 0 if strings are equal. You should do:

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

i.e.:

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.

solved strncmp don’t work as it should