wc

Count words in <50 lines of C
Log | Files | Refs | README

commit c3ac29d88737dea8b98f6ba7ddf442d5b276b2fd
parent 926eab1a9bf94b69f9ffb8fb8f937af8292faad8
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Fri, 15 Sep 2023 12:19:40 +0300

tweak: use else if instead of wrapped else, if

+formatting

Diffstat:
Msrc/extra/chc/chc.c | 4++--
Msrc/extra/lc/lc.c | 8++++----
Msrc/wc | 0
Msrc/wc.c | 20+++++++++-----------
4 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/src/extra/chc/chc.c b/src/extra/chc/chc.c @@ -6,7 +6,7 @@ int chc(FILE* fp) register int c; int num_chars = 0; while ((c = getc(fp)) != EOF) { - num_chars ++; + num_chars++; } printf("%i\n", num_chars); return 0; @@ -22,7 +22,7 @@ int main(int argc, char** argv) perror("Could not open file"); return 1; } - return chc(fp) && fclose(fp); + return chc(fp) && fclose(fp); } else { printf("Usage: chc file.txt\n"); printf(" or: cat file.txt | chc\n"); diff --git a/src/extra/lc/lc.c b/src/extra/lc/lc.c @@ -6,11 +6,11 @@ int lc(FILE* fp) int num_lines = 0; register int c; while ((c = getc(fp)) != EOF) { - if (c == '\n' ) { - num_lines ++; + if (c == '\n') { + num_lines++; } } - // num_lines += (c != '\n'); // < judgment call, adds a line if the last line isn't followed by a newline. + // num_lines += (c != '\n'); // < judgment call, adds a line if the last line isn't followed by a newline. printf("%i\n", num_lines); return 0; } @@ -25,7 +25,7 @@ int main(int argc, char** argv) perror("Could not open file"); return 1; } - return lc(fp) && fclose(fp); + return lc(fp) && fclose(fp); } else { printf("Usage: lc file.txt\n"); printf(" or: cat file.txt | lc\n"); diff --git a/src/wc b/src/wc Binary files differ. diff --git a/src/wc.c b/src/wc.c @@ -8,14 +8,12 @@ int wc(FILE* fp) while ((c = getc(fp)) != EOF) { if (c != ' ' && c != '\n' && c != '\t') { word = 1; - } else { - if (word) { - num_words++; - word = 0; - } + } else if (word) { + num_words++; + word = 0; } } - num_words+=word; + num_words += word; printf("%i\n", num_words); return 0; } @@ -30,11 +28,11 @@ int main(int argc, char** argv) perror("Could not open file"); return 1; } - int wc_status = wc(fp); - int fclose_status = fclose(fp); - return (wc_status == 0) && (fclose_status ==0); - // can't do return wc_status == 0 && fclose_status == 0; - // because then if wc returns with -1, fclose is not called. + int wc_status = wc(fp); + int fclose_status = fclose(fp); + return (wc_status == 0) && (fclose_status == 0); + // can't do return wc_status == 0 && fclose_status == 0; + // because then if wc returns with -1, fclose is not called. } else { printf("Usage: wc file.txt\n"); printf(" or: cat file.txt | wc\n");