twentieth exercise

This commit is contained in:
celso 2022-11-14 21:29:38 -03:00
parent e3a93001ef
commit 00efba9193
1 changed files with 46 additions and 0 deletions

46
1-20.c Normal file
View File

@ -0,0 +1,46 @@
#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;
}