khalfella

c_ch01_ex12.c

Sep 20th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. /*
  2. * Exercise 1-12.
  3. * Write a program that prints its input one word per line.
  4. */
  5.  
  6. #include <stdio.h>
  7. #define IN 1 /* inside a word */
  8. #define OUT 0 /* outside a word */
  9.  
  10. /* count lines, words, and characters in input */
  11.  
  12. main() {
  13. int c, state;
  14. state = OUT;
  15.  
  16. while ((c = getchar()) != EOF) {
  17. if (c == ' ' || c == '\n' || c == '\t') {
  18. if (state == IN) {
  19. state = OUT;
  20. putchar('\n');
  21. }
  22. } else {
  23. state = IN;
  24. putchar(c);
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment