-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1-22.c
70 lines (65 loc) · 1.31 KB
/
exercise1-22.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
66
67
68
69
70
#include <stdio.h>
#define TABSTOP 8
#define MAXLINE 1000
#define LINELENGTH 64
int getLine(char line[], int length);
int entab(char in[], char out[], int length);
void clearArray(char arr[], int len);
void foldLine(char in[], int length);
int main(){
char line[MAXLINE];
char out[MAXLINE];
int c;
while((c=getLine(line, MAXLINE)) != EOF){
foldLine(line, c);
printf("%s", line);
}
return 0;
}
/* I plan to replace the last blank in the line with a newline char.
* If a blank does not exist, then I will insert a newline
*/
void foldLine(char in[], int len){
int lineLength = 1;
int blank = -1;
int i;
for(i = 0; i < len; ++i){
if(in[i] == ' '){
blank = i;
++lineLength;
}else if(in[i] == '\t'){
blank = i;
++lineLength;
lineLength += TABSTOP - (lineLength % TABSTOP);
}else{
++lineLength;
}
if(lineLength >= LINELENGTH){
if(blank != -1){ // replace most recent blank
in[blank] = '\n';
}else{ // no tabs or spaces found
int j;
for(j = len; j > i; --j){
in[j] = in[j-1];
}
in[i] = '\n';
++len;
in[len] = '\0';
}
blank = -1;
lineLength = 1;
}
}
}
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;
}