#define MAXLEN 50/*2009/09/19晚 写于白鹿原*/ typedef struct /*Brute-Force算法实现*/ { char ch[MAXLEN]; int len; }SString; void StrAsign(SString *s)//串的建立函数 { int i,j; char c; printf("/nPlease input the length of String:"); scanf("%d",&j); getchar(); printf("/nplease input the value of String:"); for(i=0;i<j;i++) { scanf("%c",&c); s->ch[i]=c; } s->len=j; } void output(SString *s)//串的输出 { int i; for (i=0;i<s->len;i++) printf("%c",s->ch[i]); printf("/n"); } int StrIndex(SString *s,int pos,SString t)//从主串s的下标pos起,串t第一次出现的位置,成功返回位置序号,不成功返回-1// { int i,j,start; if(t.len==0)return 0; start=pos; i=start; j=0; while(i<s->len&&j<t.len) if(s->ch[i]==t.ch[i]) { i++; j++; } else { start++; i=start; j=0; } if(j>=t.len) return start; else return -1; } void main() { int pos,flag,len; SString *s1; SString s2; s1=(SString *)malloc(sizeof(SString)); s1->len=0; printf("/nNOW Please input the String1!"); StrAsign(s1); printf("/nNOW Please input the String2!"); StrAsign(&s2); printf("/nInput the value of pos(must pos<S1.Len):"); scanf("%d",&pos); flag=StrIndex(s1,pos,s2); if(flag=-1) printf("/nThe operate is not OK!"); else printf("/nThe operae is ok!"); printf("/nThe index is %d",flag); }