Your idea works, but you don't really need firstpart and secondpart as you can write directly to fullstring. Here's my example:
Code:
/* Interpolate src by substituting placeholder with the decimal string
* representation of value, writing the result, including the terminating null
* character, to the first n chars of dest, returning dest.
*/
char *interpolate(char *dest,
size_t n,
const char *src,
const char *placeholder,
int value)
{
if (n > 0)
{
char *p = strstr(src, placeholder);
if (p)
{
size_t first_segment_len = (size_t)(p - src);
if (first_segment_len >= n)
{
first_segment_len = n - 1;
}
dest[0] = '\0';
strncat(dest, src, first_segment_len);
snprintf(dest + first_segment_len,
n - first_segment_len,
"%d%s",
value,
p + strlen(placeholder));
}
else
{
dest[0] = '\0';
strncat(dest, src, n - 1);
}
}
return dest;
}
Applying the above to your example, you would call interpolate like this:
Code:
interpolate(fullstring, MAX_LEN, "hello$$hello", "$$", 123);