我很难在一个句子中替换整个单词。

例如:

更换:

  • thea
  • hellohi
  • housetree

  • 输入:



    输出:



    是否可以仅使用<string.h>库而不使用Regex等来实现?

    最佳答案

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct pair {
        char *key;
        char *value;
    } Pair;
    
    int cmp(const void *a, const void *b){
        return strcmp(((const Pair*)a)->key, ((const Pair*)b)->key);
    }
    
    char *dictionary(const char *word){
        static const Pair table[] =
            {{"hello", "hi"}, {"house", "tree"},{"the", "a"}};
        char *p, *wk = strdup(word);
        Pair key, *pair;
    
        for(p=wk;*p;++p)
            *p = tolower(*p);
        key.key = wk;
        pair=bsearch(&key, table, sizeof(table)/sizeof(Pair), sizeof(Pair), cmp);
        free(wk);
        if(pair){
            wk=strdup(pair->value);
            if(isupper(*word))
                *wk = toupper(*wk);//capitalize
            return wk;
        }
    
        return NULL;
    }
    
    typedef char Type;
    
    typedef struct vector {
        size_t size;
        size_t capacity;
        Type *array;
    } Vector;
    
    Vector *vec_make(){
        Vector *v;
        v = (Vector*)malloc(sizeof(Vector));
        if(v){
            v->size = 0;
            v->capacity=16;
            v->array=(Type*)realloc(NULL, sizeof(Type)*(v->capacity += 16));
        }
        return v;
    }
    
    void vec_add(Vector *v, Type value){
        v->array[v->size] = value;
        if(++v->size == v->capacity){
            v->array=(Type*)realloc(v->array, sizeof(Type)*(v->capacity += 16));
            if(!v->array){
                perror("memory not enough");
                exit(-1);
            }
        }
    }
    void vec_adds(Vector *v, Type *values, size_t size){
        while(size--)
            vec_add(v, *values++);
    }
    
    char *convert(const char *str){
        static const char *delimiters = " \t\n,.!?";
        char *in = strdup(str);
        char *out;
        char *inp = in;
        Vector *v = vec_make();
    
        while(*inp){
            size_t size;
            if(size = strcspn(inp, delimiters)){
                char *word, *cnv_word;
                word = malloc(size+1);
                memcpy(word, inp, size);
                word[size]='\0';
                if(NULL==(cnv_word = dictionary(word)))
                    vec_adds(v, word, size);
                else
                    for(out = cnv_word; *out; ++out)
                        vec_add(v, *out);
                free(word);
                free(cnv_word);
                inp += size;
            }
            if(size = strspn(inp, delimiters)){
                vec_adds(v, inp, size);
                inp += size;
            }
        }
        vec_add(v, '\0');
        out = v->array;
        free(v);
        free(in);
    
        return out;
    }
    
    int main(void){
        char input[] = "Hello there, this is the house.";
        char *output = convert(input);
    
        puts(input);
        puts(output);
        free(output);
    
        return 0;
    }
    /* result
    Hello there, this is the house.
    Hi there, this is a tree.
    */
    

    09-08 04:32