replacing pieces of string

后端 未结 3 1993
星月不相逢
星月不相逢 2021-01-22 18:19

i\'m doing something like excel, i have something like this:

1         2           3
A1        B1          C1

where it replaces the content for

3条回答
  •  一生所求
    2021-01-22 18:35

    I think that as may be replaced with values ​​by table lookup by cutting the name simply its corresponding value in the name of the string you want to replace.

    E.g

    #include 
    #include 
    #include 
    
    
    typedef struct pair {
        char key[16];
        int  value;
    } Pair;
    
    int main(void){
        Pair var_table[] = { { "A1", 1 }, {"B1", 2}, { "C1", 3 }};//sorted
        size_t size = sizeof(var_table)/sizeof(*var_table);
        char input[256] = "A1+B1+C1";
        char outbuff[4096];
        char *p;
        int offset = 0;
    
        printf("This -> %s\n\n", input);
        for(p=input;*p;){
            Pair *var;
            char op, *opp;
    
            opp=strpbrk(p, "+-*/");
            if(opp){
                op = *opp;
                *opp = '\0';//cut string at op position
            }
            //search key(p)
            var = (Pair*)bsearch(p, var_table, size, sizeof(*var), (int (*)(const void *, const void *))strcmp);
            if(var)//find!
                offset += sprintf(outbuff + offset, "%d", var->value);//or store array? 
            else
                offset += sprintf(outbuff + offset, "%s", "#UNKNOWN_VAR_NAME#");
            if(opp){
                offset += sprintf(outbuff + offset, "%c", op);
                p = opp + 1;
            } else 
                break;
        }
        printf("to -> %s\n", outbuff);
    
    
        return 0;
    }
    

提交回复
热议问题