summaryrefslogtreecommitdiffstats
path: root/3/3.c
blob: c9721f28dd7504e46a2990244b2d752673c6dd34 (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
#include <ctype.h>
#include <stdio.h>

#define MAXLEN 1000

/* expands shorthand notation like a-z and 0-9 for ASCII charset */
void expand(char s1[], char s2[]);

int main(int argc, char *argv[]) {
  int i;
  char c, s1[MAXLEN], s2[MAXLEN];

  for (i = 0; i < MAXLEN - 1 && (c = getchar()) != '\n' && c != EOF; i++)
    s1[i] = c;
  s1[i] = 0;

  expand(s1, s2);
  printf("%s\n", s2);

  return 0;
}

void expand(char s1[], char s2[]) {
  int i, j, c;

  for (i = 0, j = 0; j < MAXLEN - 1 && (c = s1[i]) != 0; i++) {
    if (c == '-' && i > 0 && isalnum(s1[i-1]) && isalnum(s1[i+1])) {
      c = s1[i-1] + 1;
      while (c < s1[i+1])
        s2[j++] = c++;
      continue;
    }

    s2[j++] = c;
  }

  s2[j] = 0;
}