forked from lacrypta/colimba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
46 lines (34 loc) · 800 Bytes
/
main.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
/* Exercise 1-18. Write a program to remove trailing blanks and tabs from each
line of input, and to delete entirely blank lines.*/
#include <stdio.h>
#define MAXSIZE 1000
int getline(char s[], int lim);
int main() {
int k;
char line[MAXSIZE+1]; /* +1 for \0 */
while ((k = getline(line, MAXSIZE+1)) > 0){
printf("%s.", line);
}
return 0;
}
int getline(char s[], int lim){
int c, i = 0, count = 0;
while (((c = getchar()) != '\n') && c != EOF) {
if (i < lim-1) {
s[i] = c;
++i;
}
++count;
}
if (0 < i) {
--i;
--count;
while ((s[i] == ' ') || (s[i] == '\t')) {
--i;
--count;
}
++i;
s[i] = '\0';
}
return count;
}