Here is the description: Modify the program detab (written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments.
What I know about tabs, when I hit a tab I travel to the next tab stop. For example, assume tab stops (8, 16, 24, 32) etc so if I'm in column 12 and hit a tab stop I go to column 16. That is, what this exercise is looking for I assume.
Just now I'm having hard time figuring out how should I do this, any pseudocode/other help is appreciated.
Here is my version of detab, by the way, if it is any help here:
Code:
#include <stdio.h>
#define MAXLINE 1000
#define SPACE ' '
#define TAB '\t'
/* Write a program 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 symbolic constant? */
int countspaces(int offset, int tabsize)
{
return tabsize - (offset % tabsize);
}
int main(void)
{
int pos, c;
while((c = getchar()) != EOF)
{
if(c == TAB)
{
int numSpaces = countspaces(pos, 5); /* n is a symbolic constant here, not a variable */
for(int i = 0; i < numSpaces; ++i)
{
putchar(SPACE);
++pos;
}
}
else if( c == '\n')
{
putchar(c);
pos = 0;
}
else
{
putchar(c);
++pos;
}
}
}