diff options
| author | Sadeep Madurange <smadurange@users.noreply.github.com> | 2022-01-02 18:19:39 +0800 |
|---|---|---|
| committer | Sadeep Madurange <smadurange@users.noreply.github.com> | 2022-01-02 18:19:39 +0800 |
| commit | 66682d77d344ac0d40c780ea59efcfc3801a6985 (patch) | |
| tree | 8f22862f0bfc4c5714f0719f796c556eebaa884d /5/7.c | |
| parent | 1fdc86998f97f32b0e3e7b10ac5d75becaee5e60 (diff) | |
| download | k&r-exercises-66682d77d344ac0d40c780ea59efcfc3801a6985.tar.gz | |
5.7
Diffstat (limited to '5/7.c')
| -rw-r--r-- | 5/7.c | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -0,0 +1,41 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define MAXLEN 1000 +#define MAXLINES 5000 + +int readlines(char **lines, int maxlines); + +int main() { + int i, count; + char **lines; + + lines = malloc(sizeof(char *) * MAXLINES); + + for (i = 0; i < MAXLINES - 1; i++) + lines[i] = malloc(sizeof(char) * MAXLEN); + count = readlines(lines, MAXLINES); + + for (i = 0; i < count; i++) + printf("L%d: %s\n", i + 1, *lines++); + + return 0; +} + +int readlines(char **lines, int maxlines) { + int i, len; + char *s; + + for (i = 0; i <= maxlines; i++) { + s = fgets(s, MAXLEN, stdin); + if (!s) + return i; + len = strlen(s); + strncpy(*lines, s, len - 1); + *lines[len] = 0; + lines++; + } + + return i; +}
\ No newline at end of file |
