So..I tried it again...this time my code does not seem to be doing anything. I think I have the concept down. However my code is wrong....I think the
must be intefering with my getline function...can somebody help me please?
Code:
#include <stdio.h>
#define MAXLINE 10
int getLine(char line[], int maxline);
void copy(char to[], char from[]);
//prints longest input line
main(){
int len; //current line length
int max; //maximum length seen so far
int possiblemax; //possible maximum length
int realmax; //the real maximum length
int c; //the char
char line[MAXLINE]; //current input line
char longest[MAXLINE]; //longest line saved here
max = possiblemax = realmax = 0;
while ((len = getline(line, MAXLINE)) > 0)
//checks to see if array ends with a new line
if (line[len - 1] != '\n'){
//while c is not end of file
while ((c = getchar()) != EOF){
//if c is not equal to a new line
if (c != '\n'){
//while c is not a new line add one to possible max
while(c != '\n'){
++possiblemax;
}
}
//else if is equal to a new line
else if (c == '\n'){
//if possiblemax is greater than real max
//set realmax to possiblemax
//reset possible max
if (possiblemax > realmax){
realmax = possiblemax;
possiblemax = 0;
}
//otherwise reset possiblemax
else possiblemax = 0;
}
}
}
if (len > max){
max = len;
copy(longest, line);
}
if (max > 0){ //there was a line
printf("length:%d", realmax);
putchar('\n');
printf("%s", longest);
}
return 0;
}
//read a line into s, return length
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 into to: assume to is big enough
void copy(char to[], char from[]){
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}