//2.在终端输入多行信息,找出包含“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 ch=0; int i=0; int limit=MAX-1; while ((ch = getchar())&&(--limit)&&(ch!=‘\n‘)&&(ch!=EOF)) { line[i++]=ch; } if(ch==‘\n‘) { line[i++]=‘\n‘; } line[i]=‘\0‘; return i; } char *strstr(char line[],char *s2) { int i=0; int j=0; int k=0; for(i=0;line[i]!=‘\0‘;i++) { for(j=0,k=i;(s2[j]!=‘\0‘)&&(line[k]==s2[j]);k++,j++) { ; } if((j>0)&&(s2[j]==‘\0‘)) { return &line[i]; } } return NULL; } int main() { char line[MAX]; char *p="ould"; while(getline(line)) { if(strstr(line,p)) { printf("%s",line); } } system("pause"); return 0; } 运行结果如下:
原文:http://760470897.blog.51cto.com/10696844/1705626