# Thread: Kernigan and Ritchie Exercise 5-11

1. ## Kernigan and Ritchie Exercise 5-11

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;
}
}

}```

2. Well it's looking for you to turn main into
int main ( int argc, char *argv[] )

And for you to run the program with a command line parameter, say
./mydetab --stops="8,16,24,32,40" filename

Which would be current behaviour, or
./mydetab --stops="5,10,15,20,25,30" filename

So if you were at column 12, then the next stop would be 16 in the first case, and 15 in the second.

3. Thank you @Salem. Can you be more detailed about the code that should be written? It's a bit of time I have written the above code, and my C programming is also rusty (Not to mention the breaks that I have kept from time to time).