-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1-20.c
53 lines (48 loc) · 879 Bytes
/
exercise1-20.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#define TAB 4
#define MAXLINE 1000
int getLine(char line[], int length);
int detab(char in[], char out[], int length);
int main(){
char line[MAXLINE];
char out[MAXLINE];
int c;
while((c=getLine(line, MAXLINE)) != EOF){
c = detab(line, out, c);
printf("%s", out);
for(;c>=0;--c){
out[c]='\0';
}
}
return 0;
}
int detab(char in[], char out[], int len){
int i;
int index = 0; // index is for out array
for(i=0; i < len; ++i){
if(in[i] == '\t'){
out[index]= ' ';
++index;
while(index % TAB !=0){ // fill in rest of column
out[index] = ' ';
++index;
}
}else{
out[index] = in[i];
++index;
}
}
return index;
}
int getLine(char line[], int len){
int c, i;
for(i=0; i < len-1 && (c=getchar()) != EOF && c != '\n'; ++i){
line[i] = c;
}
if(c == '\n'){
line[i] = c;
++i;
}
line[i]='\0';
return i;
}