samir82show

c_ch01_ex20.c

Oct 24th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. /*Exercise 1-20. Write a program detab that
  2. replaces tabs in the input with the proper
  3. number of blanks to space to the next tab
  4. stop. Assume a fixed set of tab stops, say
  5. every n columns. Should n be a variable or
  6. a symbolic parameter?*/
  7. #include <stdio.h>
  8. #define MAX 1000
  9. char str[MAX];
  10. int detab(int len);
  11. int main()
  12. {
  13. int len;
  14. while ((len = detab(MAX)) > 0)
  15. printf("%s\n", str);
  16. return 0;
  17. }
  18. int detab(int len) {
  19. int c, i, j, t, temp, remainder;
  20. i = t = j = remainder = 0;
  21. while ((c = getchar()) != EOF && c != '\n' && i < len - 1 ) {
  22. if (i > 8)
  23. remainder= i % 8;
  24. if (c != '\t') {
  25. if (t) {
  26. if (i > 8) {
  27. while (j < (t - remainder)) {
  28. str[i++] = ' ';
  29. j++;
  30. }
  31. } else {
  32. temp = i;
  33. while (j < (t - temp)) {
  34. str[i++] = ' ';
  35. j++;
  36. }
  37. }
  38. t = j = 0;
  39. }
  40. str[i++] = c;
  41. } else
  42. t += 8;
  43. }
  44. if (t) {
  45. if (i > 8) {
  46. while (j < (t - remainder)) {
  47. str[i++] = ' ';
  48. j++;
  49. }
  50. } else {
  51. temp = i;
  52. while (j < (t - temp)) {
  53. str[i++] = ' ';
  54. j++;
  55. }
  56. }
  57. }
  58. str[i] = '\0';
  59. return i;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment