Substring replacement

Basic types / Strings

//replaces every occurrence of from (not empty) with to,
//returns a new string: the caller frees it
char *replace(const char *s, const char *from, const char *to) {
    size_t fromLength = strlen(from);
    size_t toLength = strlen(to);

    size_t count = 0;
    for (const char *p = strstr(s, from); p != nullptr; p = strstr(p +
                          fromLength, from)) {
        count++;
    }

    char *result = malloc(strlen(s) - count * fromLength + count * toLength +
                          1);
    char *out = result;
    const char *p;
    while ((p = strstr(s, from)) != nullptr) {
        memcpy(out, s, p - s);
        out += p - s;
        memcpy(out, to, toLength);
        out += toLength;
        s = p + fromLength;
    }
    strcpy(out, s);
    return result;
}

char *startString = replace("3, 2, 1, go!", "1", "one");
char *temp = startString;
startString = replace(temp, "2", "two");
free(temp);
temp = startString;
startString = replace(temp, "3", "three");
free(temp);
//startString is "three, two, one, go!"

printf("startString is %s\n", startString);
free(startString);