C function to replace substring in string using malloc--no string functions
<p>Right now I can replace the word <code>indifferent</code> in the given string with <code>nonchalant</code> but I need to make this function dynamic so <code>indifferent</code> can be replaced with any word. I know i need to use malloc to create a new array that will hold my original string with the new word but don't have a strong understanding of how to use malloc yet, Please explain how to use malloc properly in this situation.  Thank you.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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 &lt; 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;
};
</code></pre>