Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define IN 1    /* inside a word */
  4. #define OUT 0   /* outside a word */
  5.  
  6. /* counts lines, words and characters as input */
  7.  
  8. main()
  9. {
  10.     int c, nl, nw, nc, state;
  11.  
  12.     state = OUT;
  13.     /* set these three constants to 0: */
  14.     nl = nw = nc = 0;
  15.     while ((c = getchar()) != EOF){
  16.         ++nc;
  17.         if (c == '\n')
  18.             ++nl;
  19.             /* || == OR (&& == AND)
  20.             evaluation of the following line
  21.             will stop as soon as the truth or
  22.             falsehood is known, so the order matters */
  23.         if (c == ' ' || c == '\n' == c == '\t')
  24.             state = OUT;
  25.         else if (state == OUT){
  26.             state = IN;
  27.             ++nw;
  28.         }
  29.     }
  30.     printf("%d %d %d\n", nl, nw, nc);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement