-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-18.c
85 lines (73 loc) · 2.17 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
print all input lines but get rid of trails
*/
#include <stdio.h>
#define MAXLINE 60 /* maximum input of size */
#define CHARNUM 0 /* Minimum Character Number set 0 to inhibit any empty line */
int getLineNoTrails(char line[], int maxline);
void copyAppend(char to[], char from[], int j);
int main(){
int len; /* line length by getchar */
char line[MAXLINE]; /* current input line - string */
char allLines[MAXLINE]; /* lappended input line */
int j;
j = 0;
printf ("This will print out any input lines without whitespace or tub trails \n");
while ((len = getLineNoTrails (line, MAXLINE)) > 0)
{
copyAppend (allLines, line, j);
// printf ("%d \n", j);
// donot overwrite '\n'
j = j + len + 1;
}
if (j<3){
printf ("Error: please put input more than 3 \n");
}
else{
// printf ("%d j is \n", j);
// printf ("%d len is \n", len);
// over write '\n'
--j;
printf ("%d \n", j);
allLines[j] = '\0';
printf("%s \n", allLines);
}
return 0;
}
/*
getLineNoTrails: read a line return int - the length without any ' ' or '\t' on trail
(changed the name from getline as stdio.h has a declared func with the same name)
arg[0]: char array - input text
arg[1]: int - the maimum limit of the length of input text (arg [0])
*/
int getLineNoTrails(char a[], int lim){
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++){
a[i] = c;
}
--i;
while ((a[i] == ' ') || (a[i] == '\t')){
a[i] = '\0';
i--;
}
// return the length
// add '\0' for all
i++;
a[i] = '\0';
return i;
}
/*
copyAppend: read a line append onto
arg[0]: char array - where appending to
arg[1]: char array - to get appended onto arg[0]
arg[2]: int - index of arg[0] where appending from
*/
void copyAppend (char to[], char from[], int j){
int i;
i = 0;
while ((to[j] = from[i]) != '\0'){
++i;
++j;
}
to[j] = '\n';
}