如何在 C 中替换指定次数的 cstring?

How to replace a specified number of occurrences of a cstring in C?

Python 等语言有 replace(s, old, new [,maxreplace]) 方法,它不仅可以替换子字符串,还可以限制替换的次数。我如何在不使用 Regex 的情况下在 C 中实现它?最好不要修改 cstring,并且可以是任何长度,包括 0。这是我尝试过的。我希望这个函数有一个参数 max_replacements 这样当且仅当它等于 0 时,所有出现的地方都会被替换。

char *strnrep(char *haystack, char needle, char replacement, int max_replacements) {
    char buf[strlen(haystack) + 1];
    int count = 0;
    strcpy(buf, haystack);
    for (unsigned j = 0; j < strlen(buf); ++j) {
        if (buf[j] == needle) count++;
    }
    int j = 0;
    if (max_replacements == 0) {
        max_replacements = count;
    }
    while (j < max_replacements) {
        for (int i = 0; buf[i] != '[=10=]'; i++) {
            if (buf[i] == needle) {
                buf[i] = replacement;
                break;
            }
        }
        j++;
    }
    return strdup(buf);
}

但仅适用于 char 而不是 char*char[]...

NOTE 这个问题 NOT 与其中一位评论者的建议重复。我要求指定的出现次数而不是全部。

正在更新previous replace solution

不要寻找所有发生的事件,如果您达到限制,则停止:

        if (count >= max)
            break;
// You must free the result if result is non-NULL.
char *str_replace(const char *orig, char *rep, char *with, int max) {
    char *result; // the return string
    const char *ins;    // the next insert point
    char *tmp;    // varies
    int len_rep;  // length of rep (the string to remove)
    int len_with; // length of with (the string to replace rep with)
    int len_front; // distance between rep and end of last rep
    int count;    // number of replacements

    // sanity checks and initialization
    if (!orig || !rep)
        return NULL;
    len_rep = strlen(rep);
    if (len_rep == 0)
        return NULL; // empty rep causes infinite loop during count
    if (!with)
        with = "";
    len_with = strlen(with);

    // count the number of replacements needed
    ins = orig;
    for (count = 0; tmp = strstr(ins, rep); ++count) {
        ins = tmp + len_rep;
        if (count >= max)
            break;
    }

    tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);

    if (!result)
        return NULL;

    // first time through the loop, all the variable are set correctly
    // from here on,
    //    tmp points to the end of the result string
    //    ins points to the next occurrence of rep in orig
    //    orig points to the remainder of orig after "end of rep"
    while (count--) {
        ins = strstr(orig, rep);
        len_front = ins - orig;
        tmp = strncpy(tmp, orig, len_front) + len_front;
        tmp = strcpy(tmp, with) + len_with;
        orig += len_front + len_rep; // move to next "end of rep"
    }
    strcpy(tmp, orig);
    return result;
}
int main(void)
{
    const char* input = "foo bar one bar two bar three bar";
    char *replace = str_replace(input, "bar", "john", 2);
    printf("before: %s\nafter: %s\n", input, replace);
    free(replace);
    return 0;
}

会输出

before: foo bar one bar two bar three bar
after: foo john one john two bar three bar