This is my version of detab, from this K&R exercise:
Modify detab to accept a list of tab stops as arguments. Use the default tab setting if there are no arguments.
#include <stdio.h>
#include <stdlib.h>
#define TAB_STOP 8
/* replaces tabs from input with the proper amount of blank spots */
int Detab()
{
int c, x;
int column;
x = column = 0;
while((c=getchar())!=EOF)
{
if(c == '\n') /* reseting counter if newline */
{
putchar(c);
return 1;
}
else if(c!='\t') /* column counts places to tab spot */
{
putchar(c);
column++;
if(column == TAB_STOP)
column = 0;
}
else /* tab */
{
for(x=0; x<TAB_STOP - column; x++)
putchar('_');
column = 0;
}
}
return 0;
}
int main(int argc, char *argv[])
{
int valid;
while((valid=Detab())!=0);
printf("Press any key to continue.\n");
getchar();
return 0;
}
My question is if there are more then one argument—for example 5, 8, 10—when is the next tab stop suppose to start being active? At which point should program start using TAB_STOP 8 instead of the starting 5? After a newline or how should I do this?
I'm also not really sure if I should put all of this into main, or should I stick with a separate function?
Edit: ok this is what i tried.
#define MAX_ARGUMENTS 100
int main(int argc, char *argv[])
{
int i, val = 0;
int nums[MAX_ARGUMENTS];
int x = 0;
for(i = 1; i < argc; i++) {
while(isdigit(*argv[i])) {
val = val * 10 + *argv[i] - '0';
*++argv[i];
}
nums[x++] = val;
val = 0;
}
Detab(nums);
printf("Press any key to continue.\n");
getchar();
return 0;
}
Am i on the right track? Can this work? I still havent modified detab.
I'd interpret TABSTOP 5 8 10 to mean there are tab stops at the 5th, 8th, and 10th columns (and after that, every 8 columns, or whatever you're using as the default. It's open to question whether the next tab stop after column 10 should be at column 18 (8 spaces later) or 16 (the next multiple of the default 8).