Hey everyone, I am currently working through the K&R book trying to teach myself C and one of the exercises is confusing me. The exercise is this:
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 a symbolic parameter?
I think I understand what it is asking and have written a program that seems to work. I wrote the program to display the modified tabs rather than changing the string entered itself though I don't think it would be too difficult to switch in any case. Here is the function I wrote:
Code:
void printstring(char string[], int n)
{
int currentcolumn=1;
int i, x;
for(i=0; string[i]!='\0'; i++, currentcolumn++)
{
if(string[i]=='\t')
for(x=0; currentcolumn%n!=0; x++)
{
putchar(' ');
currentcolumn++;
}
else
putchar(string[i]);
if(currentcolumn==80 || string[i]=='\n')
currentcolumn=0;
}
}
Does anyone have any suggestions on how that could be improved or changed? Or maybe I am missing the point of the exercise entirely . I am very new to C so any suggestions would be greatly appreciated.
Thanks!