I am trying to do a split of a string and then insert another string into the 1st string at the point of split.
Example:
int main(int argc, char **argv)
{
char src1[4]= "foo";
char src2[4]= "bar";
}
I would like to split the src1 as f and oo to insert src2, so that I get a single string fbaroo. What is the best way to do it in C?
I tried using snprintf, but I am not able to achieve the same. Following is the code:
snprintf(result, 1,"%s",src1[0]);
snprintf(result, strlen(src2), "%s",src2);
snprintf(result, strlen(src1)-1, "%s", **how do i get remaining characters**);
Ofcourse, I can have it initially split to combine later, but I am trying to find if there is a better solution i.e. using library functions ?
The
snprintfs in your code all all overwriting the buffer from the start.Here’s one way to do it with one call
The
&src1[1]shows how to get “the rest of the string.”