samir82show

c_ch01_ex21.c

Oct 27th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. /*
  2. *Exercise 1-21. Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same *spacing. Use the same tab stops as for detab. When either a tab or a single blank would suffice to reach a tab stop, which should *be given preference?
  3. */
  4. #include <stdio.h>
  5. #define MAX 1000
  6. char str[MAX];
  7. int entab(int len);
  8. int main()
  9. {
  10. int len;
  11. while ((len = entab(MAX)) > 0)
  12. printf("%s\n", str);
  13. return 0;
  14. }
  15. int entab(int len) {
  16. int c, i, bl, mod, j, lc;
  17. i = bl = lc = 0;
  18. while ((c = getchar()) != EOF && c != '\n' && i < len - 1 ) {
  19. lc++;
  20. if (c == ' ') {
  21. bl++;
  22. if ((lc % 8) == 0) {
  23. str[i++] = '\t';
  24. bl = 0;
  25. }
  26. } else {
  27. if (bl) {
  28. for (j = 0;j < bl;j++)
  29. str[i++] = ' ';
  30. bl = 0;
  31. }
  32. str[i++] = c;
  33. }
  34. }
  35. str[i] = '\0';
  36. return i;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment