solutions/The C Programming Language/1.17.c

63 lines
1.0 KiB
C

#include <stdio.h>
// 80 chars line is too long to test so i shorten to 10
#define MAXLINE 30
#define MAXTEXT 3000
#define SELECTION_LENGTH 10
// output all strings that are longer than MAXLINE
int _getline(char line[], int maxline);
int copy(char to[], int head, char from[]);
main()
{
int len;
int longest_lines_head;
char line[MAXLINE];
char longest_lines[MAXTEXT];
longest_lines_head = 0;
while ((len = _getline(line, MAXLINE)) > 0 && longest_lines_head < MAXTEXT)
if (len > SELECTION_LENGTH)
{
longest_lines_head = copy(longest_lines, longest_lines_head, line);
}
printf("%s\n", longest_lines);
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;
}
int copy(char to[], int head, char from[])
{
int from_i;
int to_i;
from_i = 0;
to_i = head;
while ((to[to_i] = from[from_i]) != '\0' && to_i < MAXTEXT - 1)
{
++to_i;
++from_i;
}
to[to_i] = '\n';
return to_i + 1;
}