非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 != '
' && --limit > 0 )
11     {
12         s[i++] = c;
13     }
14 
15     if (c == '
')
16     {
17         s[i] = '';
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] !=''; i++)
27     {
28         for (j = i,k = 0;t[k] !='' && s[j] == t[k] ;j++,k++)
29         {
30             ;
31         }
32         if (k>0 && t[k] == '')
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
",line);
51             cout<<line<<endl;
52         }
53     }
54 }
原文地址:https://www.cnblogs.com/yaoxc/p/3324799.html