-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path1-18.c
66 lines (50 loc) · 1.21 KB
/
1-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Exercise 1-18. Write a program to remove trailing blanks and tabs from each
* line of input, and to delete entirely blank lines.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
/* functions */
int getLine(char [], int);
int rightTrim(char [], int);
/* getLine function: read a line into s, return length */
int getLine(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int rightTrim(char s[], int len)
{
int nw = 0;
if (s[--len] == '\n') {
s[len] = '\0'; /* remove newline character */
nw = 1; /* set flag */
}
while (--len >= 0 && (s[len] == ' ' || s[len] == '\t'))
s[len] = '\0';
++len;
if (nw)
s[len] = '\n'; /* add back the newline character */
return ++len;
}
int main(void)
{
int len; /* current line length */
char line[MAXLINE]; /* current input line */
while ((len = getLine(line, MAXLINE)) > 0) {
len = rightTrim(line, len);
if (len == 1 && line[0] == '\n') /* delete if line is empty */
line[0] = '\0';
printf("%s", line);
}
return 0;
}