khalfella

c_ch01_ex11.c

Sep 20th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. /*
  2. * Exercise 1-11.
  3. * How would you test the word count program?
  4. * What kinds of input are most likely to uncover bugs if there are any?
  5. */
  6.  
  7. #include <stdio.h>
  8. #define IN 1 /* inside a word */
  9. #define OUT 0 /* outside a word */
  10.  
  11. /* count lines, words, and characters in input */
  12.  
  13. main() {
  14. int c, nl, nw, nc, state;
  15. state = OUT;
  16. nl = nw = nc = 0;
  17.  
  18.  
  19. while ((c = getchar()) != EOF) {
  20. ++nc;
  21. if (c == '\n')
  22. ++nl;
  23. if (c == ' ' || c == '\n' || c == '\t')
  24. state = OUT;
  25. else if (state == OUT) {
  26. state = IN;
  27. ++nw;
  28. }
  29. }
  30.  
  31. printf("%d %d %d\n", nl, nw, nc);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment