/* write a program to make a function to compare two string */
#include<stdio.h>
char * xstrstr(char *,char *);
main()
{
char s1[100],s2[100],*p;
puts("enter the first string:");
gets(s1);
puts("enter the second string:");
gets(s2);
p=s1;
if(xstrstr(p,s2)==NULL)
{
printf("%s not found in %s",s2,s1);
}
else
{
printf("%s is found in %s",s2,s1);
}
}
char * xstrstr(char*s1, char*s2)
{
int i,j;
for(i=0;s1[i];i++)
{
if(s1[i]==s2[0])
{
for(j=1;s2[j];j++)
{
if(s1[i+j]!=s2[j])
break;
}
if(s2[j]=='\0')
break;
}
}
if(s1[i]=='\0')
return NULL;
else
return s1+i;
}
No comments:
Post a Comment