How to repeat a string N times in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char s[] = "c c++";
    int len_s = strlen(s);
    int totaltimes = 5;

    char *p = (char *)malloc(len_s * sizeof(char) * totaltimes + 1);

    for (int i = 0; i < totaltimes; i++) {
         strcat(p, s);
    }

    puts(p);

    free(p);

    return 0;
}




/*
run:

c c++c c++c c++c c++c c++

*/

 



answered Feb 1, 2022 by avibootz
edited Feb 1, 2022 by avibootz

Related questions

3 answers 267 views
1 answer 87 views
87 views asked Dec 23, 2024 by avibootz
1 answer 207 views
3 answers 311 views
1 answer 199 views
1 answer 111 views
1 answer 99 views
99 views asked Dec 23, 2024 by avibootz
...