-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx118.c
64 lines (47 loc) · 1.33 KB
/
x118.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
/* 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 MAXLINE 10000
int stripline(char s[], int lim);
main()
{
char s[MAXLINE];
int i = 0, len;
while((len = stripline(s, MAXLINE)) > 0)
{
/* this if statement is the part that "deletes entirely blank lines" */
if (len > 1) printf("%s", s);
}
}
/* getline: read a line into s, return length */
/* s includes the \0 but len only give number of chars inculding the \n but not
* the \0 */
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;
s[i] = '\0';
return i;
}
/* stripline: read a line into s and "end" it by putting a \n\0 after the last
* non whitespace char */
int stripline(char buf[], int lim)
{
/* read a whole line into the buffer */
int i;
if ((i = getline(buf, lim) - 1) == -1)
return 0;
while(buf[i] == 0x20 || buf[i] == '\n' || buf[i] == '\t')
--i;
buf[++i] = '\n'; /* put the newline back */
buf[++i] = '\0';
return i ;
}
/* here are some trailing spaces */
/* there are two blank lines after this one */
/* here are some trailing tabs and spaces */