Chapter 4 - Functions and Program Structure
第4章 函数与程序结构
4.1 Basics of Functions
4.1 函数的基本知识
模式匹配的例子:
#include<stdio.h>
int getline(char *s1, int lim){ char *sp; sp = s1; while(lim > 1 && (*sp = getchar()) != EOF && *sp != '\n'){ lim--; sp++; } if(*sp == '\n') sp++; *sp = '\0'; return sp - s1; }
int strindex(const char *s1, const char *s2){ const char *s3, *sp1, *sp2; s3 = s1; while(*s1){ sp1 = s1; sp2 = s2; while(*sp1 == *sp2 && *sp1 != '\0'){ sp1++; sp2++; } if(*sp2 == '\0') return s1 - s3; s1++; } }
int main(){ char s1[300], s2[100]; char c; scanf("%s", s2);
while((c = getchar()) == '\n') ; ungetc(c, stdin); while(getline(s1, 300) > 0){ if(strindex(s1, s2) >= 0) printf("%s", s1); } }
|
阅读(386) | 评论(0) | 转发(0) |