So, I have written a mystrstr() function which should behave exactly as the original strstr() function. I have tested huge amount of cases and my function seems to work. However, it does not pass some of the tests of the online submission system.
Can you help me?
int mystrcmp(char *a, char *b)
{
int n = mystrlen(a);
int m = mystrlen(b);
int l = n;
if (m<n) l = m;
//printf("strcmp => %d %d\n", n, m);
for (int i=0; i<l; i++)
{
//printf("%c %c\n",a[i],b[i]);
if (a[i]<b[i]) return -2;
else if (a[i]>b[i]) return 2;
}
if (n<m) return -1;
else if (n>m) return 1;
else return 0;
}
char *mystrstr(char *haystack, char *needle)
{
int n = mystrlen(haystack);
int m = mystrlen(needle);
if (n==0&&m==0) return haystack;
int result;
for (int i=0; i<=(n-m); i++)
{
result = mystrcmp(haystack+i, needle);
if (result==1||result==0||result==-1)
return haystack+i;
}
return NULL;
}