fgets()
which has not been introduced
by page 34. I've fixed the solution to use K&R's getline() function instead. Further thanks to
Roman Yablonovsky who, in Oct 2000, pointed out that the solution was buggy, and hinted at a
fix. Basically, the problem he spotted was that my solution failed to keep track of the cumulative
effect of multiple tabs in a single line. I've adopted his fix (which was in fact also slightly
buggy, but I've fixed that too).
detab
that replaces tabs in the input with the proper
number of blanks to space to the next tab stop. Assume a fixed set of tab
stops, say every n columns. Should n be a variable or
a symbolic parameter?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUFFER 1024
#define SPACE ' '
#define TAB '\t'
int CalculateNumberOfSpaces(int Offset, int TabSize)
{
return TabSize - (Offset % TabSize);
}
/* K&R's getline() function from p29 */
int getline(char s[], int lim)
{
int c, i;
for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if(c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int main(void)
{
char Buffer[MAX_BUFFER];
int TabSize = 5; /* A good test value */
int i, j, k, l;
while(getline(Buffer, MAX_BUFFER) > 0)
{
for(i = 0, l = 0; Buffer[i] != '\0'; i++)
{
if(Buffer[i] == TAB)
{
j = CalculateNumberOfSpaces(l, TabSize);
for(k = 0; k < j; k++)
{
putchar(SPACE);
l++;
}
}
else
{
putchar(Buffer[i]);
l++;
}
}
}
return 0;
}