#include <stdio.h>
/*对输入的一行字符串做模版匹配,有匹配则输出*/
int getline(char line[], int max);
int strindex(char source[], char searchfor[]);
//#include "subfunctions.h"
#define MAXLINE 1000
char pattern[] = "ould";
int main()
{
char line[MAXLINE];
int found = 0;
//不断的读取一行,有模版匹配则输出
while(getline(line, MAXLINE) > 0)
if(strindex(line, pattern) >= 0){
printf("%s", line);
found++;
}
return found;
}
#include <stdio.h>
int getline(char s[], int lim)
{
int c, i;
i = 0;
//对最长行和文件结束有单独处理
//行的最大长度由存储数组的大小决定
//输入从getchar得到
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
s[i++] = c;
if(c=='\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int strindex(char s[], char t[])
{
int i, j, k;
for(i = 0; s[i] != '\0'; i++){
//对原来的字符串重新取下标,扫描完做匹配的模版,则匹配成功
for(j = i, k = 0; s[j] == t[k]; j++, k++)
;
if(k > 0 && t[k] == '\0')
return i;
}
return -1;
}