Exercise 1-21. Write a program
entabthat 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?
The code I wrote for the exercise:
#include <stdio.h>
#define TAB 8
void entab(int space);
int main()
{
int c, i;
i = 0;
while ((c = getchar()) != EOF)
{
if (c == ' ')
++i;
else if (c == '\t')
i = i + TAB;
else
{
entab(i);
i = 0;
putchar(c);
}
}
return 0;
}
void entab(int space)
{
int i;
for (i = 1; space >= TAB; ++i)
if (i == TAB)
{
putchar('\t');
i = 0;
space = space - TAB;
}
for (i = 0; i < space; ++i)
putchar(' ');
}
But when I give it the input "3 blank spaces + a tab + 5 blank spaces + j" the output I get is longer than the input :
The first line in the picture is the input and the second line is the output
Update:
Rewrote the code based on one of the comments and one of the answers into this:
#include <stdio.h>
#define TAB 8
void entab(int space);
int main()
{
int c, i, l;
i = l = 0;
while ((c = getchar()) != EOF)
{
++l;
if (c == ' ')
++i;
else if (c == '\t')
i = i + TAB - (l%8);
else
{
entab(i);
i = 0;
putchar(c);
}
if (c == '\n')
l = 0;
}
return 0;
}
void entab(int space)
{
int i;
for (i = 0; space >= TAB; ++i)
if (i == TAB)
{
putchar('\t');
i = -1;
space = space - TAB;
}
for (i = 0; i < space; ++i)
putchar(' ');
}
but now the problem is that it's always one characters less than the input:


The output you are seeing is longer than the input because the program is replacing each space with a tab and some spaces. You should modify the
entabfunction to replace strings of blanks with a tab followed by the appropriate number of spaces.