diff --git a/The C Programming Language/1.16.c b/The C Programming Language/1.16.c new file mode 100644 index 0000000..88d96c9 --- /dev/null +++ b/The C Programming Language/1.16.c @@ -0,0 +1,51 @@ +#include + +#define MAXLINE 10 + +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; +}