-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_1-16.c
executable file
·69 lines (56 loc) · 1.3 KB
/
exercise_1-16.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 70 /* maximum input line size */
int mygetline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line and its length */
main() {
int len; /* current line length */
int max; /* maximum line length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line so far */
max = 0;
while ((len = mygetline(line, MAXLINE)) > 0) {
if (len > max) {
max = len;
copy(longest, line);
}
}
if (max > 0) {
/* there was at least one line */
printf("%d : %s\n", max, longest);
}
return 0;
}
/* mygetline: read a line into s (up to lim characters), return length */
int mygetline(char s[], int lim) {
int c, i;
/* count all the characters but newline. Put all we can (lim) into s */
for (i=0; (c=getchar()) != EOF && c!='\n'; i++) {
if (i<lim) {
s[i] = c;
}
}
/* We stopped. Get the newline */
if (c=='\n') {
if (i<lim) {
s[i] = c;
}
i++;
}
/* add the null terminator to s */
if (i<lim) {
s[i] = '\0';
} else {
/* ran out of space, cut it off */
s[lim-1] = '\0';
}
return i;
}
/* copy: copy 'from' into 'to'. Assume 'to' is big enough. */
void copy(char to[], char from[]) {
int i;
i=0;
while ((to[i] = from[i]) != '\0') {
i++;
}
}