M
Marcelo De Brito
Hi!
I've implemented a K&R's book (2nd edition) example from chapter 1
(the program that takes some text lines and gives you back the longest
line's length), but it seems it is not working.
When I press CTRL+D (EOF), the program should quit and give me back
the length of the longest line typed. However, it does not happen
anyway.
If possible, please, read the code below and if you find some bug, let
me know.
Here you are the code:
#include<stdio.h>
#define MAXLINE 1000 //maximum input line lenght
int mygetline(char line[], int maxline);
void mycopy(char to[], char from[]);
int main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while((len = mygetline(line, MAXLINE)) > 0)
if(len > max)
{
max = len;
mycopy(longest, line);
}
if(max > 0)
printf("%s", longest);
return(0);
}
int mygetline(char s[], int lim)
{
int c, i;
for(i = 0; ((i < lim - 1) && ((c = getchar()) != EOF) && (c !=
'\n')); ++i)
s = c;
if(c == '\n')
{
s = c;
++i;
}
s = '\0';
return(i);
}
void mycopy(char to[], char from[])
{
int i;
while((to = from) != '\0')
++i;
}
Best Regards!
Marcelo
I've implemented a K&R's book (2nd edition) example from chapter 1
(the program that takes some text lines and gives you back the longest
line's length), but it seems it is not working.
When I press CTRL+D (EOF), the program should quit and give me back
the length of the longest line typed. However, it does not happen
anyway.
If possible, please, read the code below and if you find some bug, let
me know.
Here you are the code:
#include<stdio.h>
#define MAXLINE 1000 //maximum input line lenght
int mygetline(char line[], int maxline);
void mycopy(char to[], char from[]);
int main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while((len = mygetline(line, MAXLINE)) > 0)
if(len > max)
{
max = len;
mycopy(longest, line);
}
if(max > 0)
printf("%s", longest);
return(0);
}
int mygetline(char s[], int lim)
{
int c, i;
for(i = 0; ((i < lim - 1) && ((c = getchar()) != EOF) && (c !=
'\n')); ++i)
s = c;
if(c == '\n')
{
s = c;
++i;
}
s = '\0';
return(i);
}
void mycopy(char to[], char from[])
{
int i;
while((to = from) != '\0')
++i;
}
Best Regards!
Marcelo