fork 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 + 1));
  9.  
  10. if (result == NULL) {
  11. return NULL;
  12. }
  13.  
  14. for(size_t i = 0; i < count; i++) {
  15. strncpy(result + i*len, src, len);
  16. }
  17.  
  18. result[count * len] = '\0';
  19. return result;
  20. }
  21.  
  22. int main(void)
  23. {
  24. char * result = repeat_str(3, "hello ");
  25. if (result == NULL) {
  26. printf("OOM\n");
  27. return EXIT_FAILURE;
  28. }
  29. int success = strcmp(result, "hello hello hello ") == 0;
  30. printf("Result: '%s' (success: %d)", result, success);
  31. return success ? EXIT_SUCCESS : EXIT_FAILURE;
  32. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Result: 'hello hello hello ' (success: 1)