Code:
void expand(char s1[], char s2[])
{
int j, i;
int len = strlen(s1);
for (i=j=0; i < len; i++)
{
int processed = 0;
char from = s1[i];
char to = s2[i+2];
if (s1[i+1] =='-')
if ((isalpha(from) && isalpha(to))||(isdigit(from) && isdigit(to)))
{
while (from <= to)
{
s2[j++]=from++;
processed = 1;
}
}
if (!processed)
s2[j++] = s1[i];
s2[j] = '\0';
}
}
You need to sit down and decide *exactly* what you want this function to do...
You are feeding it two strings... from and to ... where is your output?
Your function will fail if s2 is a string literal.
Also you are not inserting characters... if you fed it "hello 1-9 world" ... you would get back "hello 123456789" not "hello 123456789 world".
What you need is a function to parse the input string, another to get the expansion string and yet another function to insert that string into your existing string...
Code:
int Expand(char *dst, const char from, const char to)
{ char x, i = 0;
for( x = from; x <=to; x++)
{ dst[i] = x;
i++ }
return i; }
int Insert(char *dst, char *src, int pos, const char *ins)
{ int i;j;
int x = strlen(ins) + pos;
for (i = 0; i < pos; i++)
*dst = *src;
for (j = pos; j < x; j++
*dst = *ins;
while (*src != 0)
*dst[j++] = *src[pos++];
return 1; }
int Parse(char *str)
{ int i, len = strlen(str);
char ins[64];
for (i = 0; i < len; i++)
{ if (str[i] == '-')
{ Expand(ins,str[i-1],str[i+1]);
Insert(str,str,i-1,ins); } }
return strlen(str); }
Now this is just "top of the head" it's not going to work first try... but that's about the way I'd go at it...
Hint: Think in smaller blobs...