fork(3) download
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. char * repeat_str(size_t count, const char * src)
  6. {
  7. size_t len = strlen(src);
  8. char * result = (char *)malloc(sizeof(char) * len * count);
  9.  
  10. for(size_t i = 0; i < count; i++)
  11. strncat(result, src, len);
  12.  
  13. return result;
  14. }
  15.  
  16. int main(void)
  17. {
  18. char * result = repeat_str(4, "hello ");
  19. printf("Result: '%s' (success: %d)", result, (strcmp(result, "hello hello hello ") == 0));
  20. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Result: 'hello hello hello hello ' (success: 0)