String Search C program without using built-in functions -


i have problem in c program. string search program. problem when type string aabaaacaamaad, result comes null when search ab in should not ab there in aabaaacaamaad. same result comes am , ad right why come aabaaacaamaad? code:

char* mystrstr(char* pszsearchstring, char* pszsearchword); int main(int argc, char* argv[]) {     char sztemp1[20] = {0};     char sztemp2[10] = {0};     char * psztemp1 = null;     strcpy(sztemp1, "aabaaacaamaad");     strcpy(sztemp2, "aa");      psztemp1 = mystrstr(sztemp1, sztemp2);     printf("%s", psztemp1);     getch();     return 0; }  char* mystrstr(char* pszsearchstring, char* pszsearchword)  {     int nfcount = 0;                         int nscount = 0;                         int nsearchlen = 0;     int nindex = 0;     char* pszdelstring = null;       if(pszsearchstring == null || pszsearchword == null) {     return null;     }         while(pszsearchword[nsearchlen] != '\0') {         nsearchlen++;     }                            if(nsearchlen <= 0){         return pszsearchstring;     }      for(nfcount = 0; pszsearchstring[nfcount] != '\0'; nfcount++) {         if(pszsearchstring[nfcount] == pszsearchword[nscount]) {            nscount++;         } else {            nscount = 0;         }            if(nscount == nsearchlen) {             nindex = (nfcount - nscount) + 1;             pszdelstring = pszsearchstring + nindex;             return pszdelstring;         }    }    return null; } 

i see code trying do, want avoid loop in loop you're missing 1 thing. when match fails you're not going still moving forward in pszsearchstring while should not. result of flaw incomplete matches skip characters. that's reason why strstr function uses loop in loop every character in pszsearchstring there new loop match pszsearchword. here original strstr.c file bsd/darwin:

char * strstr(const char *in, const char *str) {     char c;     size_t len;      c = *str++;     if (!c)         return (char *) in; // trivial empty string case      len = strlen(str);     {         char sc;          {             sc = *in++;             if (!sc)                 return (char *) 0;         } while (sc != c);     } while (strncmp(in, str, len) != 0);      return (char *) (in - 1); } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -