solutions/The C Programming Language/1.16.c

55 lines
779 B
C
Raw Permalink Normal View History

2024-05-01 03:40:22 -04:00
#include <stdio.h>
#define MAXLINE 10
// forcing getline to output actual amount of characters
// from input even if it exceedes MAXLINE boundary
2024-05-01 03:40:22 -04:00
int _getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = _getline(line, MAXLINE)) > 0)
if (len > max)
{
max = len;
copy(longest, line);
}
if (max > 0)
printf("%d: %s\n", max, longest);
return 0;
}
int _getline(char s[], int lim)
{
int c, i, j;
for (i = 0; (c = getchar()) != '\n'; ++i)
{
if (i < lim - 1)
s[i] = c;
}
j = i;
if (j > lim - 1)
j = lim - 1;
s[j] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}