I need to insert a string in another string at a specific place. Here's a simple example:
char *a = "Dany S.";
char *b = "My name is *a , I come from ... ";
So, in string b in place of *a I expect to have Dany S.
How to do that ?
The best/easiest way would be to use standard C conventions:
char *a = "Dany S.";
char *b = "My name is %s, I come from...";
char *c = malloc(strlen(a) + strlen(b));
sprintf( c, b, a );
Then c contains your new string. When you're done with c, you will need to free the memory:
free( c );
If you want to use c in an output that terminates the line, then you can declare b as:
char *b = "My name is %s, I come from...\n";
+1 for the null terminator.malloc the OP is in charge to free that memory chunk pointed by c, otherwise he may leak memory (he seems to be a newbie).You could use printf i.e.:
#include <stdio.h>
char *a = "Dany S.";
char *b = "My name is %s , I come from ... ";
printf(b, a);
snprintf.