- /* k&r(7.7): Line Input and Output
-
created on Mar 27, 2011
-
*/
-
#include "stdio.h"
-
-
-
#define MAXLINE 1000 /* maximum input line length */
-
-
int max; /* maximum length seen so far */
-
char line[MAXLINE]; /* current input line */
-
char longest[MAXLINE]; /* longest line saved here */
-
int getline1(char *, int);
-
void copy(void);
-
-
/* print the longest input line; stdardard library version */
-
main()
-
{
-
int len;
-
//extern int max;
-
int max;
-
extern char longest[];
-
extern char line[];
-
max = 0;
-
while ((len = getline1(line, MAXLINE)) > 0)
-
if (len > max) {
-
max = len;
-
copy();
-
}
-
if (max > 0) /* there was a line */
-
printf("%s", longest);
-
return 0;
-
}
-
-
/* getline: read a line into s, return length; implement from std fgets() */
-
int getline1(char *line, int max)
-
{
-
if(fgets(line, max, stdin) == NULL)
-
return 0;
-
else
-
return strlen(line);
-
}
-
-
/* copy: copy 'from' into 'to'; assume to is big enouth */
-
void copy(void)
-
{
-
int i;
-
extern char line[], longest[];
-
i = 0;
-
while ((longest[i] = line[i]) != '\0')
-
++i;
-
}
阅读(660) | 评论(0) | 转发(0) |