fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char **concancate_string(const char *name[], const char *subject[], size_t n) {
  6. char **destin = malloc(n * sizeof *destin);
  7. for (int i = 0; i < n; i++) {
  8. destin[i] = malloc(strlen(name[i]) + strlen(subject[i]) + 3 + 1); // add space for " : " and terminating '\0'
  9. sprintf(destin[i], "%s : %s", name[i], subject[i]);
  10. }
  11. return destin;
  12. }
  13.  
  14. int main(void) {
  15. char *name[] = {"michel", "sam", "roy", "romi"};
  16. char *subject[] = {"physics", "math", "chemistry", "biology"};
  17. char **foo = concancate_string(name, subject, 4);
  18. for (int i = 0; i < 4; i++) printf("%s\n", foo[i]);
  19. for (int i = 0; i < 4; i++) free(foo[i]);
  20. free(foo);
  21. return 0;
  22. }
  23.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
michel : physics
sam : math
roy : chemistry
romi : biology