C函数使用malloc替换字符串中的子字符串 - 没有字符串函数

时间:2016-03-01 18:42:16

标签: c string function malloc

现在我可以用indifferent替换给定字符串中的单词nonchalant,但我需要将此函数设置为动态,以便indifferent可以替换为任何单词。我知道我需要使用malloc创建一个新数组,用新单词保存我的原始字符串,但是对于如何使用malloc还没有很好的理解,请解释在这种情况下如何正确使用malloc。谢谢。

#include <stdio.h>
#include <stdlib.h>

int findPosition(char string[], char sub[]) {
    int i = 0;
    int j = 0;
    int f = 0;

    for (i = 0; string[i] != '\0'; i++) {
        if (sub[j] == string[i]) {
            if (sub[j + 1] == '\0') {
                f = 1;
                break;
            }
            j++;
        } else
            j = 0;
    }
    if (f == 1) {
        return i - j;
    }
    return -1;
}

int findLength(char sub[]) {
    int i = 0;

    for (i = 0; sub[i] != '\0'; i++) {

    }
    return i;
};

void replaceWord(char string[], char sub[], char replace[]) {
    int i = 0;
    int j = 0;
    int p = findPosition(string, sub);
    int l = findLength(sub);
    int k = p + l - 1;

    for (i = p; i < k; i++) {
        string[i] = replace[j];
        j++;
    }
    while(string[k] != '\0') {
        string[k] = string[k + 1];
        k++;
    }
}

int main(int argc, const char *argv[]) {
    char stringArray[120] = "\"Mr.Fay, is this going to be a battle of wits?             \""
                            "\t\"If it is,\" was the indifferent retort, \""
                            "you have come unarmed!\"";

    replaceWord(stringArray, "indifferent", "nonchalant");

    int i = 0;
    while (stringArray[i] != '\0') {
        printf("%c", stringArray[i]);
        i++;
    }
    return 0;
};

1 个答案:

答案 0 :(得分:0)

您可以使用malloc为新字符串分配内存,用另一个单词替换每个单词的出现位置:

#include <stdio.h>
#include <stdlib.h>

/* use a local implementation of the string functions: */
size_t my_strlen(const char *s) {
    size_t len;
    for (len = 0; s[len] != '\0'; len++)
        continue;
    return len;
}
void *my_memcpy(void *dest, const void *src, size_t n) {
    size_t i;
    for (i = 0; i < n; i++) {
        ((unsigned char*)dest)[i] = ((unsigned char*)src)[i];
    }
    return dest;
}
char *my_strdup(const char *s) {
    size_t n = my_strlen(s) + 1;
    char *p = malloc(n);
    if (p) my_memcpy(p, s, n);
    return p;
}
char *my_strstr(const char *s1, const char *s2) {
    for (;; s1++) {
        for (size_t i = 0;; i++) {
            if (s2[i] == '\0') return s1;
            if (s1[i] != s2[i]) break;
        }
        if (*s1 == '\0') return NULL;
    }
}

char *replaceWord(const char *str, const char *s1, const char *s2) {
    char *res = my_strdup(str); /* return value is always allocated */
    char *p, *q;
    size_t offset = 0;
    size_t len = my_strlen(str);
    size_t len1 = my_strlen(s1);
    size_t len2 = my_strlen(s2);

    if (len1 == 0)
        return res;

    while ((p = my_strstr(res + offset, s1)) != NULL) {
        offset = p - res;
        if (len1 == len2) {
            /* no need to reallocate, replace in place */
            my_memcpy(res + offset, s2, len2);
        } else {                
            /* allocate a new array with the adjusted length */
            q = malloc(len + len2 - len1 + 1);
            /* copy the beginning of the string */
            my_memcpy(q, res, offset);
            /* copy the replacement string */
            my_memcpy(q + offset, s2, len2);
            /* copy the remainder of the string, and the final '\0' */
            my_memcpy(q + offset + len2, res + offset + len1, len - offset - len1 + 1);
            /* free the previous string */
            free(res);
            res = q;
        }
        /* search for matches from the end of the replacement */
        offset += len2;
    }
    return res;
}

int main(int argc, const char *argv[]) {
    char stringArray[120] = "\"Mr.Fay, is this going to be a battle of wits?             \""
                            "\t\"If it is,\" was the indifferent retort, \""
                            "you have come unarmed!\"";

    char *p = replaceWord(stringArray, "indifferent", "nonchalant");
    printf("%s", p);
    free(p);
    return 0;
}