c_exercises/1-20.c

47 lines
999 B
C
Raw Permalink Normal View History

2022-11-14 21:29:38 -03:00
#include <stdio.h>
/* 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;
}