fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char* insert(char *src, char *s, char *dst)
  6. {
  7. char *t = strdup(src + strlen(s));
  8. strcpy(src, dst);
  9. strcat(src, t);
  10. free(t);
  11. return src;
  12. }
  13.  
  14. int main(void)
  15. {
  16. char s[BUFSIZ] = "__Hello world";
  17. char *s1 = "Hi";
  18. char *s2 = "Hello";
  19. char *ptr = strchr(s, 'H');
  20.  
  21. printf("%s\n", insert(ptr, s2, s1));
  22. printf("%s\n", insert(ptr, s1, s2));
  23. printf("%s\n", insert(ptr, s2, s2));
  24. printf("%s\n", s);
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 1808KB
stdin
Standard input is empty
stdout
Hi world
Hello world
Hello world
__Hello world