//在终端输入多行信息,找出包含“ould”的行,并打印该行。
#include <stdio.h>
#define MAX 1000
int getline(char line[])
{
int limit = MAX - 1;
int ch = 0;
int i = 0;
while ((ch = getchar()) && (--limit) && ch != '\n'&&ch != EOF)
{
line[i] = ch;
i++;
}
if (ch == '\n')
{
line[i++] = '\n';
}
line[i] = '\0';
return i;
}
char *my_strstr(char line[], char *match)
{
int i, j, k;
for (i = 0; line[i] != '\0'; i++)
{
for (j = 0, k = i; match[j] != '\0'&&line[k] == match[j]; k++, j++)
{
;
}
if (j > 0 && (match[j] == '\0'))
{
return &line[i];
}
}
return NULL;
}
int main()
{
char line[MAX];
char *p = "ould";
while (getline(line))
{
if (my_strstr(line, p))
{
printf("%s", line);
}
}
return 0;
}
转载于:https://blog.51cto.com/10706198/1761713