From 00efba91939a55d6378339731adfe2269d434b2d Mon Sep 17 00:00:00 2001 From: celso Date: Mon, 14 Nov 2022 21:29:38 -0300 Subject: [PATCH] twentieth exercise --- 1-20.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 1-20.c diff --git a/1-20.c b/1-20.c new file mode 100644 index 0000000..226b35c --- /dev/null +++ b/1-20.c @@ -0,0 +1,46 @@ +#include +/* program detab + replaces tab in the input with the + proper number of blank spaces to + the next tab stop + + assume a fixed set of tab stops, + say, every n columns */ + +#define MAXLINE 1000 +#define COLPERTAB 8 + +int ggetline(char line[], int maxline){ + int character, index; + for(index = 0; index < maxline - 1 + && (character = getchar()) != EOF + && character != '\n'; index++){ + line[index] = character; + } + line[index] = '\0'; + return index; +} + +int main(){ + char line[MAXLINE]; + int index, length, column, index2; + while ((length = ggetline(line, MAXLINE)) > 0){ + column = index = 0; + while(index < length){ + if (line[index] != '\t'){ + putchar(line[index]); + column++; + } + if (line[index] == '\t'){ + index2 = COLPERTAB - (column % COLPERTAB); + column += index2; + for(; index2 > 0; index2--){ + putchar(' '); + } + } + index++; + } + putchar('\n'); + } + return 0; +}