Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *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?
- */
- #include <stdio.h>
- #define MAX 1000
- char str[MAX];
- int entab(int len);
- int main()
- {
- int len;
- while ((len = entab(MAX)) > 0)
- printf("%s\n", str);
- return 0;
- }
- int entab(int len) {
- int c, i, bl, mod, j, lc;
- i = bl = lc = 0;
- while ((c = getchar()) != EOF && c != '\n' && i < len - 1 ) {
- lc++;
- if (c == ' ') {
- bl++;
- if ((lc % 8) == 0) {
- str[i++] = '\t';
- bl = 0;
- }
- } else {
- if (bl) {
- for (j = 0;j < bl;j++)
- str[i++] = ' ';
- bl = 0;
- }
- str[i++] = c;
- }
- }
- str[i] = '\0';
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment