Ok, so I have a char stringA and char stringB, and I want to be able to insert stringB into stringA at point x.
char *stringA = "abcdef";
char *stringB = "123";
with a product of "ab123cdef"
does anyone know how to do this? Thanks in advance
Ok, so I have a char stringA and char stringB, and I want to be able to insert stringB into stringA at point x.
char *stringA = "abcdef";
char *stringB = "123";
with a product of "ab123cdef"
does anyone know how to do this? Thanks in advance
char * strA = "Blahblahblah", * strB = "123", strC[50];
int x = 4;
strncpy(strC, strA, x);
strC[x] = '\0';
strcat(strC, strB);
strcat(strC, strA + x);
printf("%s\n", strC);
Explanation:
Hope that helps.
Remark: remember to make strC long enough to contain both strA and strC, otherwise you'll produce a segmentation fault. You may do this by declaring the string as an array, like in my example.
stringA and stringB are both pointers - they contain the starting address for a blob of memory. The memory they are pointing to contain continuous strings of characters: "abcdef" and "123" respectively. Since strings are contiguous blocks memory (meaning that the memory location of a given character is one byte after the previous) you can't insert more characters into the middle of a string without first moving some characters. In your case you can't even really do this, since the amount of memory allocated for each string is exactly large enough to hold JUST that string (ignoring padding).
What you are going to have to do is copy the strings to another block of memory, one that you have allocated for that purpose, and copy them so that the second string starts x characters into the first string.
Several other SO users have posted code-solutions but I think you should try and find the exact solution on your own (and hopefully my high-level explanation of what's going on will help).
Here a more general solution.
Note that destination must have enough space in memory to add seed (e.g. an heap-allocated array of size grater than strlen(seed)+strlen(destination) ). So, concerning the question, you have to create a larger array.
/*
destination: a NULL terminated string
pos: where insert seed
seed: a NULL terminated string
*/
void insertString(char* destination, int pos, char* seed)
{
char * strC;
strC = (char*)malloc(strlen(destination)+strlen(seed)+1);
strncpy(strC,destination,pos);
strC[pos] = '\0';
strcat(strC,seed);
strcat(strC,destination+pos);
strcpy(destination,strC);
free(strC);
}
int insert_pos = 5;
int product_length = strlen(stringA) + strlen(stringB) + 1;
char* product = (char*)malloc(product_length);
strncpy(product, stringA, insert_pos);
product[insert_pos] = '\0';
strcat(product, stringB);
strcat(product, stringA + insert_pos);
...
free(product);
strlcpy (product, ..., sizeof(product). If you find that cumbersome, make yourself a Macro for it: #define STRCPY(a,b) strlcpy (a, b, sizeof(a)). Same for strcat etc.!There are plenty of answers here. However, most do rely on strcat() instead of using efficiently the length computed for the allocation, none is secure, and none offers a solution for in-place insertion.
Just copy the different string components to their target address, avoiding to iterate over them with strcat(), considering that computing once their length is sufficient. Keep in mind that malloc() might return NULL:
char* strmake_insert (const char *str, size_t pos, const char *insertstr)
{
size_t sl = strlen(str), il = strlen(insertstr);
char *newstr=malloc(sl+il+1);
if (newstr) {
memcpy (newstr, str, pos);
memcpy (newstr+pos, insertstr, il);
memcpy (newstr+pos+il, str+pos, sl-pos);
}
return newstr;
}
Example of use:
char *s = strmake_insert ("abcdef", 3, "123"); printf ("New string: %s\n", s); free(s);
I leave as an exercise the strengthening of this version into a secure one.
We'll assume here that there is enough space in the buffer allocated for the original string. Use memcpy() that can deal with overlapping arrays, whereas strcpy() is not guaranteed to do this properly. This code assumes however that the string to insert does not overlap with the original string:
void strinsert (char *str, size_t pos, const char *insertstr)
{
char *where =str+pos;
memcpy(where+strlen(insertstr), where, strlen(where)+1);
memcpy(where, insertstr, strlen(insertstr));
}
Example of use:
char mystring[100];
strcpy (mystring, "abcdef");
strinsert (mystring, 3, "123");
printf ("Altered string: %s\n", mystring);
This is however an insecure version, as the check for sufficient space must be performed elsewhere and buffer overflows are quite common unfortunately.
The following code does not use strnlen_s() and memcpy_s() which would additionally check for border cases with NULL pointers or zero length, as these functions are not available on a number of implementations. But this one already protects against common buffer size problems:
int strinsert_s (char *str, size_t pos, size_t strsz,
const char *insertstr, size_t inssz)
{
char *where =str+pos;
size_t wheresz = strsz-pos; // maximum size at insert point
size_t insertlen = strnlen(insertstr,inssz);
if (pos>=strsz || pos>=strlen(str) ) // if insert pos invalid
return EOVERFLOW;
size_t wl = strnlen(where,wheresz);
if (wl<wheresz) // if null terminated take the trailing '\0'
wl++;
if (wheresz<=insertlen || wheresz-insertlen<=wl) // if insert string too large
return EOVERFLOW;
memcpy (where+insertlen, where, wl);
memcpy (where, insertstr, insertlen);
return 0;
}
Example of use:
char mystring[100];
strcpy (mystring, "abcdef");
errno_t err = strinsert_s (mystring, 3, sizeof(mystring), "123",4);
if (err)
printf ("Error %d\n", err);
else printf ("Altered string: %s\n", mystring);
Example of buffer overflow prevented:
char mystring[9]; // the trailing '\0' will go out of buffer - nasty!
strcpy (mystring, "abcdef");
int err = strinsert_s (mystring, 3, sizeof(mystring), "123",4);
if (err)
printf ("Error %d\n", err);
else printf ("Altered string: %s\n", mystring);