Advertisement
alansam

count words

Apr 2nd, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. $ cat countwords.c
  2.  
  3. #include<stdio.h>
  4.  
  5. #define IN 1
  6. #define OUT 0
  7.  
  8. int main() {
  9.   int c, nl, nw, nc, state;
  10.   state = OUT;
  11.   nl = nw = nc = 0;
  12.   while ((c = getchar()) != EOF) {
  13.     ++nc;
  14.     if (c == '\n')
  15.       ++nl;
  16.     if (c == ' ' || c == '\n' || c == '\t')
  17.       state = OUT;
  18.     else if (state == OUT) {
  19.       state = IN;
  20.       ++nw;
  21.     }
  22.   }
  23.   printf("%d %d %d\n", nl, nw, nc);
  24. }
  25.  
  26. $ gcc-9 -Wall -o countwords countwords.c
  27. $ cat countwords.c | ./countwords
  28. 24 74 371
  29. $ ./countwords < countwords.c
  30. 24 74 371
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement