From 6d1f4f822cf6f3282f73914874aef00c4de6d812 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Wed, 1 May 2024 12:06:33 +0400 Subject: [PATCH] 1.17 --- The C Programming Language/1.17.c | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 The C Programming Language/1.17.c diff --git a/The C Programming Language/1.17.c b/The C Programming Language/1.17.c new file mode 100644 index 0000000..2efe276 --- /dev/null +++ b/The C Programming Language/1.17.c @@ -0,0 +1,60 @@ +#include + +// 80 chars line is too long to test so i shorten to 10 +#define MAXLINE 30 +#define MAXTEXT 3000 +#define SELECTION_LENGTH 10 + +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; +}