fork download
  1. #include <stdio.h>
  2.  
  3. char *strcpy2 (char *str1, char *str2)
  4. {
  5. int total = strlen (str1) + strlen (str2) + 1;
  6. char *nstr = malloc (sizeof (char) * total);
  7. char *str = nstr;
  8. while (*str1 != '\0') *nstr++ = *str1++;
  9. while (*str2 != '\0') *nstr++ = *str2++;
  10. *nstr = '\0';
  11. printf ("size: %d\n", strlen (str));
  12. return str;
  13. }
  14.  
  15. int main (void)
  16. {
  17. char *concat = strcpy2 ("Hello, ", "World.");
  18. puts (concat);
  19. free (concat);
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
size: 13
Hello, World.