#include <stdio.h>
#include <stdlib.h>

/**
 * Exercise 3-3.  Write a function expand(s1,s2) that expands 
 * shorthand notations like a-z in the string s1 into the 
 * equivalent complete list abc...xyz in s2.  Allow for 
 * letters of either case and digits, and be prepared to 
 * handle cases like a-b-c and a-z0-9 and -a-z.
 */

char* expand(char*,char*);
char  fillrange(char,char,char*);
int   rangesize(char,char);

int main(void)
{
   /* Call stringtolower on string before passing to expand */
   expand("a-b-c0-9a-f",NULL);
}

/* ToDo: Add format checking */
char* expand(char* s1, char* s2)
{
   char start,end;
   char* range;
   int begin = 0;
   int stop  = strlen(s1)-1;
   if(s1[0] == '-') begin++;

   while(s1[begin] != '\0') {
      if(s1[begin] != '-')
         start = s1[begin++];
      end   = s1[++begin];
      range = (char*)malloc(rangesize(start,end)+1);
      printf("[%c - %c] = ",start,end);
      start = fillrange(start,end,range);
      printf("%s\n",range);
      begin++;
   }
}

char fillrange(char start, char end, char* sequence)
{
   int i = 0;
   do {
      sequence[i] = start++;
   } while(sequence[i++] != end);
   sequence[i] = '\0';
   return end;
}

int  rangesize(char start, char end)
{
   return (end - start) + 1;
}
