非KMP字符串匹配实现
1 #include <iostream> 2 using namespace std; 3 4 #define MAXLINE 1000 5 //读取一行输入 6 int getline(char s[] ,int limit) 7 { 8 int c,i; 9 i = 0; 10 while ((c = getchar()) != EOF && c != '\n' && --limit > 0 ) 11 { 12 s[i++] = c; 13 } 14 15 if (c == '\n') 16 { 17 s[i] = '\0'; 18 } 19 return i; 20 } 21 22 //返回s中与t首次匹配的位置,否则返回-1 23 int strindex(char s[] ,char t[]) 24 { 25 int i,j,k; 26 for (i = 0 ; s[i] !='\0'; i++) 27 { 28 for (j = i,k = 0;t[k] !='\0' && s[j] == t[k] ;j++,k++) 29 { 30 ; 31 } 32 if (k>0 && t[k] == '\0') 33 { 34 return i; 35 } 36 37 } 38 39 40 return -1; 41 } 42 43 void main() 44 { 45 char line[MAXLINE]; 46 while (getline(line,MAXLINE) > 0) 47 { 48 if (strindex(line,"ould") >= 0 ) 49 { 50 //printf("%s\n",line); 51 cout<<line<<endl; 52 } 53 } 54 }