-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1-21.c
66 lines (61 loc) · 1.16 KB
/
exercise1-21.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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#define TABSTOP 8
#define MAXLINE 1000
int getLine(char line[], int length);
int entab(char in[], char out[], int length);
int main(){
char line[MAXLINE];
char out[MAXLINE];
int c;
while((c=getLine(line, MAXLINE)) != EOF){
c = entab(line, out, c);
printf("%s", out);
for(;c>=0;--c){
out[c]='\0';
}
}
return 0;
}
int entab(char in[], char out[], int len){
int i=0,j; // 2 pointer for search, j will count spaces
int index = 0; // index pointer for tracking out variable
int col=1; // column counter; from 1 to TABSTOP
while(i<len){
if(in[i] == ' '){
j=i+1;
++col;
while(in[j]==' ' && col <= TABSTOP){
++j;
++col;
}
if(col > TABSTOP){ // insert tab and reset column count
out[index++]='\t';
col=1;
i=j;
}else{ // insert spaces
for(;i != j; i++){
out[index++]=' ';
}
}
}else{
++col;
if(in[i] == '\t' || col > TABSTOP){
col=1;
}
out[index++]= in[i++];
}
}
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;
}