-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex1_16.c
54 lines (43 loc) · 961 Bytes
/
ex1_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
#include <stdio.h>
#define MAXLINE 1000
int getline1(char line[], int lim);
void copy(char to[], char from[]);
int main()
{
int len, longlen;
char line[MAXLINE], longline[MAXLINE];
longlen = 0;
while ((len = getline1(line, MAXLINE)) > 0) {
printf("%d: %s", len, line);
if (len > longlen) {
longlen = len;
copy(longline, line);
}
}
printf("Longest line is %swith %d characters\n", longline, longlen);
return 0;
}
int getline1(char line[], int lim)
{
int c, i, j;
i = j = 0;
while ((c = getchar()) != EOF && (c != '\n')) {
if (i < lim - 1)
line[j++] = c;
++i;
}
if (c == '\n') {
if (i <= lim - 1)
line[j++] = c;
++i;
}
line[j] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i = 0;
while ((to[i] = from[i]) != '\0') {
++i;
}
}