Question

#include <stdio.h>
#include <stdlib.h>
#include <ctype.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;
}

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

           if(x > MAX_ARGUMENTS - 1) 
              return 0;

           nums[x++] = val;
           nums[x] = '\0';
           val = 0;
     }

     while(Detab(nums));

     printf("Press any key to continue.\n");
     getchar();
     return 0;
}

In main i put all the arguments(numbers) inside nums array and then pass it to detab. So now im interested what would be the smart way to edit detab so it works. I'm still trying to figure out for a working pseudocode but i dont really know.

The way i tought it should work is: if arguments are 5, 8, 10 then a tab inside first 4 characters leads to position 5, in 5 - 7th char leads to pos 8 etc. In case of a newline, the arguments start all over again from the begining.

Was it helpful?

Solution

The most common way is to have Detab accept a pointer (which points to an element in an array) and the length of that array:

int Detab(int* data, int len); // access data[0] through data[len - 1]

Call it like so:

void example() {
  int array[] = {5, 8, 10};
  Detab(array, 3);
  // or:
  Detab(array, sizeof array / sizeof *array); // second parameter evaluates to 3
                                              // without using a magic constant
}

Here's some pseudocode for expanding tabs:

def expandtabs_in_line(line, tabstops, default, space):
  result = ""
  for c in line:
    if c != "\t":
      result += c
    else:
      for stop in tabstops:
        if stop > len(result):
          result += space * (stop - len(result))
          break
      else:
        result += space * (default - (len(result) % default))
  return result

def expandtabs(lines, tabstops=[], default=8):
  for line in lines:
    yield expandtabs_in_line(line, tabstops, default, " ")

Try it out at codepad.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top