EX 3.3: Write a function expand(s1,s2)that expands abbreviations like az in the string s1, in equivalent full list abc...xyz in s2Allow letters of any of them and numbers and be prepared to handle cases such as a-b-cand a-z0-9and -a-z. Consider that the leading or the final is taken literally.
I am trying to solve exercise 3.3 in K & R, and this is what I have:
void expand(char s1[], char s2[]){
int i;
int j;
for(i = 0, j = 0; s1[i] != '\0'; ++i, ++j){
if(isalnum(s1[i]) && s1[i+1] == '-'){
char c = s1[i];
for(char c = s1[i]; c <= s1[i+2]; ++c, ++j){
s2[j] = c;
}
++i;
} else{
s2[j] = s1[i];
}
}
s2[j] = '\0';
}
It successfully extends any range if it is not after any other range, i.e. it does not add anything to s2 after completing the first range. If I put this statement:
printf("%c\n", c);
in the second for loop, it prints the correct characters, but does not add it to s2.
Examples of inputs and outputs:
In: akls aldio a-h 19 aodk
Out: akls aldio abcdefgh
In: 0-6 a-c lol
Out: 0123456
In: a-c-g 1okd 2-4
Out: abc
- , ?
.