#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLEN 100 typedef struct{ char ch[MAXLEN+1]; int length; }SString; //顺序串结构定义 int Index_BF(SString s,SString t){ //brute force 函数 int i = 1; int j = 1; while(i <= s.length && j <= t.length){ if(s.ch[i]==t.ch[j]){ i++; j++; }else{ i = i - j +2; j = 1; } } if(j > t.length)return i-t.length; return 0; } int main() { SString s,t; s.length = 11; t.length = 3; strcpy(s.ch,"#HELLO WORLD"); //字符从1开始,0位不用 strcpy(t.ch,"#WOR"); printf("%s\n",s.ch); printf("%s\n",t.ch); printf("Index: %d\n",Index_BF(s,t)); return 0; }
03-31
1075
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-11
1139
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-14
753
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)