Very useful operation that hasn't been merged into a function.. until now. This is supposed to insert a substring into a previously d.allocated source string.
void strapp (char *source_offset, int position, char *appendage)
{
size_t appendage_size = strlen(appendage) + 1;
char copy[strlen(source_offset)];
strcpy(copy, source_offset);
source_offset = realloc(source_offset, strlen(source_offset) + appendage_size);
memcpy(&source_offset[position], appendage, strlen(appendage));
sprintf(source_offset + (position + strlen(appendage)), ©[position]);
}
USAGE:
int main(void)
{
char *str1 = malloc(11 + 1);
sprintf(str1, "Hello World");
strapp(str1, 5, " Horrible");
printf(str1);
free(str1);
return 0;
}
Produces the output:
Hello Horrible World