-
Notifications
You must be signed in to change notification settings - Fork 0
/
printf theMAX line
88 lines (77 loc) · 1.64 KB
/
printf theMAX line
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
86
87
88
#include <stdio.h>
#define MAXLINE 20
int getline(char s[], int lim);
void copy(char to[], char from[]);
int main(void)
{
char line[MAXLINE];
char longest[MAXLINE];
char temp[MAXLINE];
int len, max, prevmax, getmore;
max = prevmax = getmore = 0;
while((len = getline(line, MAXLINE)) > 0)
{
if(line[len - 1] != '\n')
{
if(getmore == 0)
copy(temp, line);
prevmax += len;
if(max < prevmax)
max = prevmax;
getmore = 1;
}
else
{
if(getmore == 1)
{
if(max < prevmax + len)
{
max = prevmax + len;
copy(longest, temp);
longest[MAXLINE - 2] = '\n';
}
getmore = 0;
}
else if(max < len)
{
max = len;
copy(longest, line);
}
prevmax = 0;
}
}
if(max > 0)
{
printf("%s", longest);
printf("len = %d\n", max);
}
return 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;
++i;
}
else if(c == EOF && i > 0)
{
/* gotta do something about no newline preceding EOF */
s[i] = '\n';
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}