Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

Step: 1

To implement memcmp inside strstr without calling it you can use the following approach Declare vari... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Foundations of Financial Management

Authors: Stanley Block, Geoffrey Hirt, Bartley Danielsen, Doug Short, Michael Perretta

10th Canadian edition

1259261018, 1259261015, 978-1259024979

More Books

Students also viewed these Programming questions

Question

Solve e x+yi = 7.

Answered: 1 week ago