Implement the strstr function as efficiently as you can.. How do you handle errors if you had to
char * strstr (const char *haystack, const char *needle) { char *result = (char *)NULL;
if ((haystack != (char *)NULL) && (needle != (char *)NULL)) { register int i; int hl = strlen (haystack); int nl = strlen (needle);
for (i = 0; i < (hl - nl); i++) if (strncmp (haystack + i, needle, nl) == 0) { result = haystack + i; break; } }
return (result); }
Implement the strstr function
char *
strstr (const char *haystack, const char *needle)
{
char *result = (char *)NULL;
if ((haystack != (char *)NULL) && (needle != (char *)NULL))
{
register int i;
int hl = strlen (haystack);
int nl = strlen (needle);
for (i = 0; i < (hl - nl); i++)
if (strncmp (haystack + i, needle, nl) == 0)
{
result = haystack + i;
break;
}
}
return (result);
}