Hi, I'm working through the problems in K&R and I'm having difficulty with a particular one.
Code:
/*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 or trailing -
is taken literally.*/
void expand (char s1[], char s2[]){
int i = 0,j = 0;
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[x] == '-'){
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;}
So far I think that this code checks if the leading characted is a "-" and copies it and checks if the string starts with a 0 or a 9 on and ends with a 0 or a 9. However, I'm not confident at all at what I've done and any criticisms and pointers would be much welcomed. I realize I could use pointers, but i'm avoiding it just because it hasn't come up in the book quite yet. Even though their solution does use pointers.