/*
* this is the frist program exercise where the spec isn't entirely clear.
*/
#include<stdio.h>
#define MAXLINE 1000 /* maximum input line size*/
int ffgetline(char line[], int maxline);
void copy(char to[],char from[]);
/* print longest input line */
int main(void)
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while((len = ffgetline(line, MAXLINE)) > 0)
{
printf("%d: %s",len ,line);
if(len > max)
{
max = len;
copy(longest,line);
}
}
if(max > 0)
{
printf("Longest is %d characters: \n%s",max,longest);
}
printf("\n");
return 0;
}
/*ffgetline: read a line into s, return length*/
int ffgetline(char s[], int lim)
{
int c,i,j;
for(i = 0, j = 0; (c = getchar())!=EOF && c != '\n';i++)
{
if( i < lim - 1)
{
s[j++] = c;
}
}
if (c == '\n')
{
if(i <= lim-1)
{
s[j++] = c;
}
++i;
}
s[j]='\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
{
i++;
}
}
这一段当时没有看明白作者的意思,在抄写的过程中,