A
arnuld
this is the example programme from that section. it does not print the
longest line otherwise it works fine, my version has only 1 extra line
of "printf". i want to know why it is not printing the longest line:
/* K&R2 section 1.9 character arrays
example programme
STATEMENT: to take number of lines as input and print the longest line
*/
#include <stdio.h>
#define MAXLINE 1000 /* MAXIMUM length of input line */
int get_current_line(char line[], int maxline);
void copy_line(char to[], char from[]);
int main() {
int len_current;
int max_seen;
char current_line[MAXLINE]; /* current line */
char longest[MAXLINE]; /* longest line seen so far */
while((len_current = get_current_line(current_line, MAXLINE)) > 0)
{
if(len_current > max_seen)
{
max_seen = len_current;
copy_line(longest, current_line);
}
}
if(max_seen > 0)
{
printf("Length of Longest line seen so far is: %d\n", max_seen);
printf("\n Longest line is: \n%s", longest);
}
return 0;
}
int get_current_line(char s[], int max_length)
{
int c, i;
for(i=0; i < (max_length - 1) && ((c = getchar()) != EOF) && c !=
'\n'; ++i)
s = c;
if(c == '\n')
{
s = c;
++i;
}
s = '\0';
return i;
}
void copy_line(char to[], char from[])
{
int i;
while((to = from) != '\0')
++i;
}
longest line otherwise it works fine, my version has only 1 extra line
of "printf". i want to know why it is not printing the longest line:
/* K&R2 section 1.9 character arrays
example programme
STATEMENT: to take number of lines as input and print the longest line
*/
#include <stdio.h>
#define MAXLINE 1000 /* MAXIMUM length of input line */
int get_current_line(char line[], int maxline);
void copy_line(char to[], char from[]);
int main() {
int len_current;
int max_seen;
char current_line[MAXLINE]; /* current line */
char longest[MAXLINE]; /* longest line seen so far */
while((len_current = get_current_line(current_line, MAXLINE)) > 0)
{
if(len_current > max_seen)
{
max_seen = len_current;
copy_line(longest, current_line);
}
}
if(max_seen > 0)
{
printf("Length of Longest line seen so far is: %d\n", max_seen);
printf("\n Longest line is: \n%s", longest);
}
return 0;
}
int get_current_line(char s[], int max_length)
{
int c, i;
for(i=0; i < (max_length - 1) && ((c = getchar()) != EOF) && c !=
'\n'; ++i)
s = c;
if(c == '\n')
{
s = c;
++i;
}
s = '\0';
return i;
}
void copy_line(char to[], char from[])
{
int i;
while((to = from) != '\0')
++i;
}