location of a substring in a string
Hello,i have this program to search a sub string in string and tells its location but it is only telling the position of first matched character.help me if u can.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int searchsubstring(char *,char *);
int position (char *,char*);
int main()
{
char s1[10],s2[10];
int i,v,j,f=0;
printf("Enter main string : ");
gets(s1);
printf("Enter substring to be searched in main string : ");
gets(s2);
if(searchsubstring(s1,s2)!=0)
printf("\nFound");
else
printf("\nNot found");
v=position(s1,s2);
printf ("\nLocation of substring: %d",v);
}
int searchsubstring(char *s1,char *s2)
{
int f=0;
for(;*s1!='\0';)
{
if(*s2=='\0')
break;
for(;*s2!='\0';)
{
if(*s1==*s2)
{
f=1;
s1++;
s2++;
}
else
{
f=0;
s1++;
break;
}
}
}
if(f==0)
return 0;
else
return 1;
getch();
}
int position(char *s1,char *s2)
{
int f=0,x=0;
for(;*s1!='\0';)
{
if(*s2=='\0')
break;
for(;*s2!='\0';)
{
if(*s1==*s2)
{
f=1;
s1++;
s2++;
x++;
return x;
}
else
{
f=0;
s1++;
x++;
break;
}
}
}
}