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.
Could someone please give me some constructive criticism in regards to my code? I have looked at the way other people did it as well as the solution at the end of the book which was nothing like what I managed to come up with.
Code:
void expand (char s1[], char s2[]){
int i = j = 0, q;
while(s1[i] != '\0')
{
if(( i == 0 || s1[i+1] == '\0') && s1[i] == '-') //1st or last char is a "-"
{
s2[j++] = s1[i];
else if(s1[i] == '-')
{
if(s1[i-1] >= '0' && s1[i-1] <= '9') // check if the left char is a 0 or a 9 eg. 0-9, 9-0
{
if(s1[i+1] >= '0' && s1[i+1] <= '9') //check the same for the right char
{
if(s1[i-1] <= s1[i+1])
{
for(q = s1[i-1]; q <= s1[i+1]; q++)
s2[j++] = q;
}
}
else if(s1[i-1] == islower() || s1[i-1] == isupper() // check if characters are digits
|| s1[i+1] == islower || s1[i+1] == isupper())
printf("Mismatching arguements unable to expand.");
else s2[j++] = s1[i];
}
elseif(s1[i-1] == islower() || s1[i-1] == islower())
{
if(s1[i+1] == islower() || s1[i+1] == islower())
{
if(s1[i-1] <= s1[i+1])
{
for(q = s[i-1];q = s[i+1]; q++)
s2[j++] = q;
}
}
else if(s1[i-1] == isdigit() || s1[i-1] == isupper() // check if characters are lowercase
|| s1[i+1] == isdigit || s1[i+1] == isupper())
printf("Mismatching arguements unable to expand.");
else s2[j++] = s1[i];
}
elseif(s1[i-1] == isupper() || s1[i-1] == isupper())
{
if(s1[i+1] == isupper() || s1[i+1] == isupper())
{
if(s1[i-1] <= s1[i+1])
{
for(q = s[i-1];q = s[i+1]; q++)
s2[j++] = q;
}
}
else if(s1[i-1] == isdigit() || s1[i-1] == islower() //check if characters are uppercase
|| s1[i+1] == isdigit || s1[i+1] == islower())
printf("Mismatching arguements unable to expand.");
else s2[j++] = s1[i];
}
else s2[j++] = s1[i];
}
else if( s1[i+1] == '-') // check if there is a '-' at the end
{
if(s1[i] >= isdigit() || s1[i] >=isupper() || s1[i] >= islower())
;
else s2[j++] = s1[i]
}
else if(s1[i-1] != '-' && s1[i+1] != '-') //check if there is a '-' at the start
s2[j++] = s1[i];
i++;
}
s2[j] == '\0';
}