summaryrefslogtreecommitdiffstats
path: root/5/7.c
blob: 8c3697cdf366a0376c08a5503106c967c6c87ee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}