tags:

views:

155

answers:

3

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

A: 
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);
Idan K
+1  A: 
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:

  1. You declare the two strings you will be joining, plus a third string into which you will put them.
  2. You declare an integer to tell the program after how many characters you wish to insert the second string into the first.
  3. The strncpy function copies the first x characters into strC. You have to add the nul ('\0') character at the end, otherwise you'll probably get rubbish.
  4. Strcat to copy the second string.
  5. Another strcat to copy the remaining part of the first string (strA+x).

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.

mingos
ya i just used: char* strC = (char*)malloc(strlen(strA) + strlen(strB_);perfect answer thank you!
Sj
@Sj, don't forget the null char!
Nick Meyer
@Sj: it should be char * strC = malloc(strlen(strA)+strlen(strB)+1). You forgot about the additional character for the '\0'. It might seem to work, but you have a chance of overwriting a byte of something that's not your string. I'm not sure gdb will catch this, but electric fence will complain.
mingos
oh right, hah got it now, thank you!
Sj
+2  A: 

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).

Niki Yoshiuchi
this was really helpful on a conceptual level, thank you +1
Sj