I'm implementing the C "strstr" function in C. This function takes 2 string in parameter and tell whether or not, the 1st string contains the second one. However, while the result is expected to be false, it returns true. Could you give me an explanation, please ?
Here is code :
#include <stdio.h>
#include <string.h>
int searchStr(char *ch1, char *ch2);
int searchStr(char *ch1, char *ch2)
{
int i = 0;
int j = 0;
while (i < strlen(ch1) - 1)
{
while (j < strlen(ch2) - 1)
{
if(i == strlen(ch1) - 1)
{
return 0;
}
else
{
if (ch1[i] == ch2[j])
{
j++;
i++;
}
else
{
if (j > 0)
{
j = 0;
}
else
{
i++;
}
}
}
}
return 1;
}
}
int main()
{
printf("%d", searchStr("science", "sh"));
}
Regards
YT