#include <stdio.h>
#include <stdlib.h>
#define M 100
typedef struct
{
char *c;
int length;
}Str;
Str InitStr();
void CreatStr(Str *S);
int BF(Str *Ma,Str *Mo,int pos);
void main()
{
Str S1,S2;
int k,pos;
S1=InitStr();
S2=InitStr();
printf("Please input the main String:\n");
CreatStr(&S1);
printf("Please inpute the mode String:\n");
CreatStr(&S2);
printf("Please input the pos:\n");
scanf("%d",&pos);
k=BF(&S1,&S2,pos);
if(k>=pos)
printf("The location is:%5d\n",k);
else
printf("NOT FOUND!\n");
}
Str InitStr()
{
Str S;
S.c=(char*)malloc(sizeof(char)*M);
S.length=0;
return S;
}
void CreatStr(Str *S)
{
int j;
char t[100];
gets(t);
for(j=0;t[j];j++)
{
S->c[j]=t[j];
S->length++;
}
}
int BF(Str *Ma,Str *Mo,int pos)
{
int i,j;
i=pos-1;
j=0;
while(i<Ma->length&&j<Mo->length)
{
if(Ma->c[i]==Mo->c[j])
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
}
if(j>=Mo->length)
return i-Mo->length+1;
else
return -1;
}