c - String Comparison Discrepancy (Works for some cases and sometimes doesn't) -


i used following code test text detection email file received.

#include <stdio.h>  int main() {   int max_size = 100;   char email[100] ="aheanstring4strinstringzil.comg";   int pointer = 0;   int = 1;   int j = 0;   int len = 0;   int chk = 0;   char found1[7] = "string1";   char found2[7] = "string2";   char found3[7] = "string3";   char found4[7] = "string4";   char found5[7] = "string5";    char user1[7] = "string1"; // 23           char user2[7] = "string2";// 19           char user3[7] = "string3"; // 14           char user4[7] = "string4"; // 16         char user5[7] = "string5";; // 15    while (pointer != max_size && chk != 1)   {      (j = 0;j<7; j++)     {       found1[j] = *(email+pointer+j);     }      if (strcmp(found1, user1) == 0){       printf("authorized user found\n");       chk = 1;       continue;     }      (j = 0;j<7; j++)     {       found2[j] = *(email+pointer+j);     }      if (strcmp(found2, user2) == 0){       printf("authorized user found\n");       chk = 1;       continue;     }      (j = 0;j<7; j++)     {       found3[j] = *(email+pointer+j);     }      if (strcmp(found3, user3) == 0){       printf("authorized user found\n");       chk = 1;       continue;     }      (j = 0;j<7; j++)     {       found4[j] = *(email+pointer+j);     }      if (strcmp(found4, user4) == 0){       printf("authorized user found\n");       chk = 1;       continue;     }       (j = 0;j<7; j++)     {       found5[j] = *(email+pointer+j);     }      if (strcmp(found5, user5) == 0){       printf("authorized user found\n");       chk = 1;       continue;     }      pointer++;    }   printf("check %d, pointer %d\n",chk, pointer);   return 0; } 

i use above code users in email body. if user found, while loop breaks. when tried running it, included different strings in above variable (email)

i tried running first on different online c-compilers. of them had strings 1,3, , 5 working fine. (being detected)

some of them had string 2 working fine (being detected).

however, of them shared fact string2 never detected. don't know why. tried thinking of reason couldn't figure out why.

i appreciate help.

char found1[7] = "string1"; 

here found1 not valid string in c since there no nul termination. need have

char found1[8] = "string1"; 

you pass found1 strcmp() lead undefined behavior since strcmp() expects null terminated string.

or @barak manos suggested can go for

char found1[] = "string1"; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -