Mmmm
Code:
#include <stdio.h>
#include <string.h>
// insert s2 into s1 at position pos
void insertstring ( char *s1, char *s2, int pos ) {
int len1 = strlen(s1);
int len2 = strlen(s2);
int i;
// relocate the tail of s1
// len1-pos chars need to be moved up len2 places
// starting at position pos
for ( i = len1 - pos ; i >= 0 ; i-- ) {
s1[pos+len2+i] = s1[pos+i];
}
// copy s2 into the hole
for ( i = 0 ; i < len2 ; i++ ) {
s1[pos+i] = s2[i];
}
// restore the end of string
s1[len1+len2] = '\0';
}
int main ( ) {
char buff[100] = "hello world";
insertstring( buff, "abc ", 6 );
printf( "%s\n", buff );
insertstring( buff, "foo ", 0 );
printf( "%s\n", buff );
return 0;
}
Shouldn't be too hard converting it to use pointer notation....