-
Notifications
You must be signed in to change notification settings - Fork 0
/
保存最长行.c
45 lines (39 loc) · 866 Bytes
/
保存最长行.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
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char form[]);
/*打印最长的输入行*/
int main() {
int len; /*当前行长度*/
int max; /*至目前为止发现的最长行的长度*/
char line[MAXLINE]; /*当前输入的行*/
char longest[MAXLINE]; /*用于保存最长的行*/
max = 0;
//printf("%d\n", max);
while((len=getline(line,MAXLINE))>0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0) /*有一行*/
printf("%s", longest);
return 0;
}
/*getline:将一行读入s中并返回其长度*/
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;
}
s[i] = '\0';
return i;
}
/*copy:从from拷贝到to,假定to足够大*/
void copy(char to[], char from[]) {
int i=0;
while ((to[i] = from[i]) != '\0')
++i;
}