Повторение строки

Простые типы / Строки

//repeats a whole string count times into a new buffer
char *repeat(const char *s, size_t count) {
    size_t length = strlen(s);
    char *result = malloc(length * count + 1);
    for (size_t i = 0; i < count; i++) {
        memcpy(result + i * length, s, length);
    }
    result[length * count] = '\0';
    return result;
}

//one character: fill with memset and add the terminating zero
char str_v[4];
memset(str_v, '7', 3);
str_v[3] = '\0';
//str_v is "777"

char *laugh = repeat("ha", 3);
//laugh is "hahaha"

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