K & R programming in C: EX 3.3

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; // index for first string
    int j; // index for 2nd string

    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

- , ?
.

+4
3

dbush, j--, , a-c-g, for. c <= s1[i+2] c < s1[i+2]

void expand(char s1[], char s2[]){
    int i; // index for first string
    int j; // index for 2nd string

    for(i = 0, j = 0; s1[i] != '\0'; ++i, ++j){
            if(isalnum(s1[i]) && s1[i+1] == '-'){
                    char c = s1[i];
                    /* Do it c < instead of c<= */
                    for(char c = s1[i]; c < s1[i+2]; ++c, ++j){
                            s2[j] = c;
                    }
                    --j;  /* Decrement j once */
                    ++i; 
            } else {
                    s2[j] = s1[i];
            }
    }
    s2[j] = '\0';
}

P.S: OP ( , ). , a-b-c-. abc-DEF, abc--def, abc-456 .. , ( ), OP .

+1

for j - , , . 0, , .

, i - , .

:

        for(char c = s1[i]; c <= s1[i+2]; ++c, ++j){
            s2[j] = c;
        }
        ++i;

:

        for(char c = s1[i]; c <= s1[i+2]; ++c, ++j){
            s2[j] = c;
        }
        i+=2;
        j--;
+3

sps dbush , .

, . : , IOCCC. / , for. i c . s1 s2 .

:

void expand(char s1[], char s2[]){
    int i;
    int j;
    char c;
    for(i = 0, j = 0; s1[i] != '\0'; i++){
        if(isalnum(s1[i]) && s1[i+1] == '-'){
            for(c = s1[i]; c < s1[i+2]; c++){
                s2[j++] = c;
            }
            i++;
        } else{
            s2[j++] = s1[i];
        }
    }
    s2[j] = '\0';
}

j--

:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv)
{
  char s[200];
  char s2[200];
  int i;
  memset(s,0,200);
  memset(s2,0,200);
  // put some spaces in between the arguments
  for(i=1;i<argc;i++){
    // counting ommitted!
    strcat(s2,argv[i]);
    s2[strlen(s2)] = ' ';
  }
  printf("In:  %s\n",s2);
  expand(s2,s);
  printf("Out: %s\n",s);
  exit(EXIT_SUCCESS);
}

$ gcc -W -Wall -std=c11  expand.c -o expand
./expand 0-9 ASD a-z QWE a-ch-r
In:  0-9 ASD a-z QWE a-c-r 
Out: 0123456789 ASD abcdefghijklmnopqrstuvwxyz QWE abchijklmnopqr

Oh ++i et al. for: 2016.

0

All Articles