Question: (c programming) I have those two functions memcmp() and strstr()(they are working), I want to implement memcmp() inside strstr()without calling it I tried many times

(c programming) I have those two functions memcmp() and strstr()(they are working), I want to implement memcmp() inside strstr()without calling it

I tried many times but I always get no result

int memcmp(const void* s1, const void* s2,size_t n){  const unsigned char *p1 = s1, *p2 = s2;    while(n--)      if( *p1 != *p2 )      return *p1 - *p2;        else      p1++,p2++;    return 0;}char *StrSearch(const char *Haystack, const char *Needle){  size_t n = StrGetLength(pcNeedle);  while(*pcHaystack)    if(!memcmp(pcHaystack++,pcNeedle,n))      return (char *) Haystack - 1;  return 0;}

This was my code after I tried to implement it inside strstr()(doesn't work)

char *strstr(const char *Haystack, const char *Needle){  const unsigned char *p1 = (void *)Haystack++, *p2 = (void *)Needle;  size_t n = StrGetLength(Needle);  while(*Haystack)    {while(n--)    {       if (*p1 != *p2)        {          if ((*p1 - *p2))        { return (char *) Haystack - 1;}          else        {return 0;}        }      else        {        (const unsigned char *)Haystack++;      (const unsigned char *)Needle++;        }    }    }  return 0;}

Step by Step Solution

3.48 Rating (165 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To implement memcmp inside strstr without calling it you can use the following approach Declare vari... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!