summaryrefslogtreecommitdiffstats
path: root/1/13.c
blob: 8ecc5493eb0c35d814432f1db81cd79fa804d0b6 (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
42
43
44
45
46
47
48
49
#include <stdio.h>

#define MAXWRD 100

/* prints a histogram of lengths of words in the input */
int main(int argc, char *argv[]) {
  int i, j, p, c, wc, cc, size;
  int data[MAXWRD];

  p = '0';
  cc = wc = 0;

  for (i = 0; i < MAXWRD; i++)
    data[i] = 0;

  while ((c = getchar()) != EOF) {
    if (wc >= MAXWRD) {
      printf("Max word count %d exceeded\n", MAXWRD);
      return 1;
    }

    if (c == ' ' || c == '\t' || c == '\n') {
      if (p != ' ' && p != '\t' && p != '\n') {
        data[wc++] = cc;
        cc = 0;
      }
    } else {
      cc++;
    }

    p = c;
  }

  for (i = 0; i < MAXWRD; i++) {
    size = data[i];
    if (size == 0)
      break;
    
    printf("%3d: ", i + 1);

    for (j = 0; j < size; j++) {
        putchar('x');
    }

    putchar('\n');
  }

  return 0;
}