Hello

Is it possible to somehow place another string inside a string ?
Example:
char array1[4]= "!!!"
char array2[5]="joey"

//Replace e with array1

Output : jo!!!ey

Thank you for any kind of input

Recommended Answers

All 2 Replies

Yes, it's possible. The destination array needs to be large enough to hold the replacement, and the process is rather manual. You'll shift everything to the right of the matching character to make a hole, then overwrite what was in the hole with the new data:

# Find 'e'
joey
  ^

# Shift everything after 'e' to make room for 3 characters
joe  y
  ^  ^

# Fill in the hole with the source string
jo!!!y
  ^  ^

Of course, if array1 is shorter than the matching string, you'll need to do the opposite and fill in the hole that's already there:

# Find "oe"
joey
 ^

# Shift everything after "oe" to fill in excess hole
joy
  ^

# Fill in the remaining hole with '!'
j!y
  ^

Don't forget to properly move your null character as well.

commented: great help!! +1

Yes, it's possible. The destination array needs to be large enough to hold the replacement, and the process is rather manual. You'll shift everything to the right of the matching character to make a hole, then overwrite what was in the hole with the new data:

# Find 'e'
joey
  ^

# Shift everything after 'e' to make room for 3 characters
joe  y
  ^  ^

# Fill in the hole with the source string
jo!!!y
  ^  ^

Of course, if array1 is shorter than the matching string, you'll need to do the opposite and fill in the hole that's already there:

# Find "oe"
joey
 ^

# Shift everything after "oe" to fill in excess hole
joy
  ^

# Fill in the remaining hole with '!'
j!y
  ^

Don't forget to properly move your null character as well.

Thank you Narue. btw->your essay about sorting on your site was and still is a great heap of help for me in my study. Much respect!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.