0

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

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by Joey_Brown
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.

Votes + Comments
great help!!
0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.