I implemented strstr() myself,the code works fine for all strings but when the string lies in the first index of the string,it returns null.
#include<stdio.h>
const char* mystrstr(const char *str1, const char *str2);
int main()
{
const char *str1="chal bhai nikal";
const char *str2="c",*result;
result=mystrstr(str1,str2);
if(*result!=NULL)
printf("found at %d location and the value is %s.",*result, str1+*result);
else
printf("Value not found");
getchar();
printf("\n");
return 0;
}
const char * mystrstr(const char *s1, const char *s2)
{
int i,j,k,len2,count=0;
char *p;
for(len2=0;*(s2+len2)!='\0';len2++); //len2 becomes the length of s2
for(i=0;*(s1+i)!='\0';i++)
{
if(*(s1+i)==*s2)
{
for(j=i,k=0;*(s2+k)!='\0';j++,k++)
{
if(*(s1+j)==*(s2+k))
count++;
else count=0;
if(count==len2)
{
p=(char*)malloc(sizeof(char*));
*p = i;
return p;
}
}
}
}
return NULL;
}