纯C字符串替换整个单词

时间:2013-11-25 21:48:06

标签: c string replace word

我在替换句子中的整个单词时遇到了麻烦。

例如:

替换:

  • thea
  • hellohi
  • housetree

输入:

  

你好,这是房子。

输出:

  

你好,这是一棵树。

是否可以只使用<string.h>库,没有正则表达式等。

4 个答案:

答案 0 :(得分:4)

可能这可以帮到你 str-replace-c

答案 1 :(得分:3)

这完全有可能,但标准库没有任何直接支持它的功能。因此,为此,您通常使用类似strstr的内容来查找要替换的字符串的现有实例,memmove移动字符串的其余部分,覆盖原始的,需要手动或者strncpy

请注意,在这种情况下,您的替换字符串都比他们要替换的原始字符短。这意味着可以安全地完成替换。如果更换时间比原件长,您还必须做一些事情来跟踪最大字符串长度,并确保不超过它。

哦,另一个不那么小的问题,但是如果你正在搜索一个完整的单词,而不仅仅是在另一个单词中出现那个字符串,你可能想要搜索<space>word<space>,除了在字符串的开头或结尾处,所以(例如)你不会用the替换there中的a并将there中的第一个字转换为{ {1}}。

答案 2 :(得分:1)

#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.
*/

答案 3 :(得分:-1)

创建一个前后的哈希值,即你要搜索的内容以及你想要替换的内容,遍历字符串并用类似KMP或rabin karp的内容搜索单词,并在哈希表中找到你需要的内容