diff --git a/wcl b/wcl new file mode 100755 index 0000000..0016b05 Binary files /dev/null and b/wcl differ diff --git a/wcl.c b/wcl.c new file mode 100644 index 0000000..dde7760 --- /dev/null +++ b/wcl.c @@ -0,0 +1,65 @@ +#include +#include +int countLines(const char* fname); +int countBytes (const char* fname); +int countWords (const char* fname); + +int main(int argc, char *argv[]) +{ + int nlines = 0; + int nbytes = 0; + int nwords = 0; + for(int i=1; i < argc; i++) + { + nlines = countLines(argv[i]); + nbytes = countBytes(argv[i]); + nwords = countWords(argv[i]); + printf("%s: lines - %d, bytes - %d, words - %d\n", argv[i], nlines, nbytes, nwords); + } + return 0; +} + +int countLines(const char* fname) +{ + FILE* file = fopen(fname, "r"); + int lines = 0; + char buffer[1024]; + while (fgets(buffer, sizeof(buffer), file) != NULL) + {lines++;} + fclose(file); + return lines; +} + +int countBytes (const char* fname) +{ + FILE* file = fopen(fname, "r"); + int bytes = 0; + int c; + while ((c = fgetc(file)) != EOF) + { + bytes++; + } + return bytes; +} + +int countWords (const char* fname) +{ + FILE* file = fopen(fname, "r"); + int words = 0; + int c; + int is_in_word = 0; + while ((c = fgetc(file)) != EOF) + { + if (isspace(c)) + {is_in_word = 0;} + else + { + if (is_in_word == 0) + { + words++; + is_in_word = 1; + } + } + } + return words; +}