-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1-18.c
49 lines (44 loc) · 834 Bytes
/
exercise1-18.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
#include <stdio.h>
#define MAXLINE 1000
int getLine(char line[], int maxline);
int rmTrailBlanks(char line[], int length);
int main(){
int len;
char line[MAXLINE];
while((len=getLine(line, MAXLINE)) != EOF){
if(rmTrailBlanks(line,len) > 0){
printf("%s\n", line);
}
}
return 0;
}
int getLine(char s[], int lim){
int c,d,i;
for(i=0; (c=getchar()) != EOF && c != '\n'; ++i){
if(i < lim-1){
s[i] = c;
d = c;
}
}
if (d == '\n'){
s[i]=d;
++i;
}
s[i] = '\0';
if(c == EOF){
i = EOF;
}
return i;
}
int rmTrailBlanks(char line[], int len){
if(line[len] == '\0')
--len;
if(line[len] == '\n')
--len;
while(len >=0 && (line[len] == '\t' || line[len] == ' ')){
--len;// find position where first non tab or space char is
}
if(len >= 0 && line[len] != '\n')
line[++len] = '\0';
return len;
}