在终端输入多行信息,找出包含“ould”的行,并打印改行。
如:
Au,love could you and I with fate conspire
To grasp this sorry scheme of things entire,
Would not we shatter it to bitd – and then.
在终端输出上述的文字,输出
Au,love could you and I with fate conspire
Au,love could you and I with fate conspire
To grasp this sorry scheme of things entire,
Would not we shatter it to bitd – and then.
Would not we shatter it to bitd – and then.
#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); } } system("pause"); return 0; }
时间: 2024-10-13 09:19:02