/* This is the first program exercise where the spec isn't entirely
* clear. The spec says, 'Revise the main routine', but the true
* length of an input line can only be determined by modifying
* getline. So that's what we'll do. getline will now return the
* actual length of the line rather than the number of characters
* read into the array passed to it.
*/
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
int main(void)
{
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */
max = 0;
while((len = getline(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;
}
/* getline: read a line into s, return length */
int getline(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;
}
/* copy: 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;
}
}
/* Exercise 1-16 */
#include <stdio.h>
#define MAXLINE 20
int getline(char s[], int lim);
void copy(char to[], char from[]);
int main(void)
{
char line[MAXLINE];
char longest[MAXLINE];
char temp[MAXLINE];
int len, max, prevmax, getmore;
max = prevmax = getmore = 0;
while((len = getline(line, MAXLINE)) > 0)
{
if(line[len - 1] != '\n')
{
if(getmore == 0)
copy(temp, line);
prevmax += len;
if(max < prevmax)
max = prevmax;
getmore = 1;
}
else
{
if(getmore == 1)
{
if(max < prevmax + len)
{
max = prevmax + len;
copy(longest, temp);
longest[MAXLINE - 2] = '\n';
}
getmore = 0;
}
else if(max < len)
{
max = len;
copy(longest, line);
}
prevmax = 0;
}
}
if(max > 0)
{
printf("%s", longest);
printf("len = %d\n", max);
}
return 0;
}
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;
}
else if(c == EOF && i > 0)
{
/* gotta do something about no newline preceding EOF */
s[i] = '\n';
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}