Mercurial > hg > Members > nobuyasu > grep
changeset 1:80a0c0eb67e0 default tip
while
author | nobuyasu |
---|---|
date | Mon, 31 Jan 2011 12:07:24 +0900 |
parents | 60596de1cb2c |
children | |
files | read.c test.txt |
diffstat | 2 files changed, 15 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/read.c Mon Jan 31 03:50:41 2011 +0900 +++ b/read.c Mon Jan 31 12:07:24 2011 +0900 @@ -6,7 +6,6 @@ int grep(char* string, char* pattern); - int main(int argc, char **argv) { FILE *fp; @@ -14,7 +13,6 @@ char *filename; char *pattern; - if(argc > 2) { pattern = argv[1]; filename = argv[2]; @@ -30,7 +28,8 @@ while( (fgets(string, MAX_LEN - 1, fp)) != NULL){ string[strlen(string) - 1] = '\0'; - grep(string, pattern); + if(grep(pattern, string )) + puts(string); } fclose(fp); @@ -38,16 +37,19 @@ return 0; } -int grep(char* string, char* pattern) + +int grep(char* pattern, char* string) { + char *pattern_; char *str_; - char *pattern_; + do{ - str_ = string; - pattern_ = pattern; - }while(*pattern_++ == *str_++); - if(*pattern_ == '\0') - return 1; - while(*++string != '\0'){} - return 0; + pattern_ = pattern; + str_ = string; + while(*pattern_++ == *str_++) + if(*pattern_ == '\0') + return 1; + }while(*++string != '\0'); + if(*str_ == '\0') + return 0; }