# Thread: K&R 3-3 expand function

1. ## K&R 3-3 expand function

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';
}```

2. Code:
```if(( i == 0 || s1[i+1] == '\0') && s1[i] == '-') //1st or last char is a "-"
{
s2[j++] = s1[i];
} /* Missing brace */

else if(s1[i] == '-')
{```

3. Do you have code that compiles?

As click has mentioned, you're missing braces. And your indentation is presumably incorrect. if/else if's should line up like this:
Code:
```if (...) {
}
else if (...) {
}
else if (...) {
}
else {
}```
Line 3 should be
int i = 0, j = 0, q;

Line 30 (or is it 31?) should have a space between the if and the else. Same with the other elseif's.

Line 74 should be
s2[j] = '\0';