Ex 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. Arrange that a leading – is taken literally.
#include
#include
void expand(char s1[], char s2[])
{
int i = 0;
char reverse = 0;
/* check if reverse */
if (s1[0] == '-')
{
reverse = 1;
i++;
}
int l = 0;
int end, start, ready;
start = 0;
end = 0;
ready = 0;
for(; s1[i] != ' '; i++)
{
if(s1[i] != '-')
{
if (start == 0)
{
start = s1[i];
}
else
{
end = s1[i];
ready = 1;
}
}
else
{
/* for x-X-X */
if(ready == 0 && start == 0)
{
start = s1[i - 1];
l--;
}
}
if(ready == 1)
{
int k;
if(reverse == 0)
{
for(k = start; k <= end; k++, l++)
{
s2[l] = k;
}
}
else
{
for(k = end; k >= start; k--, l++)
{
s2[l] = k;
}
}
start = 0; end = 0; ready = 0;
}
}
}
int main()
{
char s1[64], s2[256];
strncpy(s1, "a-b-ca-z0-9A-Z", 64);
strncpy(s2, "", 256);
expand(s1, s2);
printf("%s => %sn", s1, s2);
return 0;
}