fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char* concat(char** array, size_t n) {
  6. size_t total = 1; // One for null terminator
  7. for (int i = 0 ; i != n ; i++) {
  8. total += strlen(array[i]);
  9. }
  10. char *res = malloc(total);
  11. char *wr = res;
  12. for (int i = 0 ; i != n ; i++) {
  13. char *rd = array[i];
  14. while ((*wr++ = *rd++))
  15. ;
  16. wr--;
  17. }
  18. return res;
  19. }
  20.  
  21. int main(void) {
  22. char *data[] = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
  23. char *s = concat(data, 8);
  24. printf("'%s'\n", s);
  25. free(s);
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
'quickbrownfoxjumpsoverthelazydog'