-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path18.c
69 lines (54 loc) · 1.48 KB
/
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
67
68
69
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline);
int trim(char line[], int len);
/* Remove trailing blanks and whitespaces, delete blank lines. */
main()
{
int len, newSize; /* current line length */
char line[MAXLINE]; /* current input line */
while ((len = getline(line, MAXLINE)) > 0) {
newSize = trim(line, len);
if (newSize > 0) {
printf("READ: %04d; NEW SIZE: %04d | %s\n", len, newSize, line);
}
}
return 0;
}
/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
int c, i, last = 0;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
/* Because of operator precedence, we don't need to check if i < lim -1 */
if (c == '\n') {
s[i] = c;
++i;
}
/* Adding the null character */
s[i] = '\0';
return i;
}
/**
* Removes trailing blanks and tabs from a line
*
* @param char line[] String from input
* @param int len String size
*
* @return int New string size
*/
int trim(char line[], int len)
{
int i = len - 1; /* Last character is always '\0' */
/* We need to delete entirely blank lines */
if ((len == 1) && line[0] == '\n') {
line[0] = '\0';
return 0;
}
while (i > 0 && (line[i] == '\t' || line[i] == ' ' || line[i] == '\n')) {
line[i] = '\0';
--i;
}
return i+1;
}