khalfella

ansi_c_pg34_ch01_ex20.c

Sep 27th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. /*
  2. * Exercise 1-20.
  3. * Write a program detab that replaces tabs in the input with the proper number
  4. * of blanks to space to the next tab stop. Assume a fixed set of tab stops, say
  5. * every n columns. Should n be a variable or a symbolic parameter?
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10.  
  11. #define TABSSTOP 8
  12.  
  13. int
  14. main() {
  15. int c;
  16. int offset = 0;
  17. int spaces;
  18.  
  19. while ((c = getchar()) != EOF) {
  20. if (c == '\t') {
  21.  
  22. spaces = TABSSTOP - (offset%TABSSTOP);
  23. offset += spaces;
  24.  
  25. while (spaces--)
  26. putchar(' ');
  27.  
  28. } else {
  29. if (c == '\n')
  30. offset = -1;
  31. offset++;
  32. putchar(c);
  33.  
  34. }
  35. }
  36. return (0);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment