如:
输入:
Au,love could youand I with fate conspire
To grasp this sorryscheme of things entire,
Would not we shatterit to bitd - and then.
输出:
Au,love could youand I with fate conspire
含有的行:Au,love could youand I with fate conspire
To grasp this sorryscheme of things entire,
Would not we shatterit to bitd - and then.
含有的行:Would not we shatterit to bitd - and then.
//在终端输入多行信息,找出包含“ould”的行,并打印改行。
#include<stdio.h>#include<stdlib.h>
#define MAXLINE 100
int getline(char s[], int max)
{
int c, i;
i = 0;
while (i < max && (c = getchar()) != EOF&&c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
int findstr(char s[], char t[])
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++)
{
for (j = i, k = 0; t[k] != '\0'&&s[j] == t[k]; j++, k++)
;
if (k > 0 && t[k] == '\0')
return 1;
}
return -1;
}
int main()
{
char p[] = "ould";
char line[MAXLINE];
while (getline(line, MAXLINE))
{
if (findstr(line, p) > 0)
printf("含有的行:%s\n", line);
}
system("pause");
return 0;
}
代码运行结果: