Well I am almost done with this exercise where I had to print the longest line from a set of input lines.
Code:
#include <stdio.h>#include <stdlib.h>
#define MAX 1000
int main()
{
int len=0,i,j,c,k;
char line[MAX];
char longest[MAX];
while ((c = getchar())!= EOF)
{
for(i=0,j=0 ; (c=getchar()) != '\n'; i++){
line[i]=c;
j++;}
if(j>len){len = j;
for(k=0;k<j-1;k++){
longest[k]=line[k];
}
}
}
printf("%d\n %s",len,longest);
}
There is just one problem. It leaves out the first and last character of the longest string. Where's my mistake?