0

Is there a way to choose the point in a char array to insert a string without deleting any existing elements of the char array?i.e to insert a string anywhere in the char array

4
  • Arrays are of a fixed size, so you really can't insert anything into them. Commented Aug 27, 2013 at 10:10
  • what if they array size is much larger than the string? Commented Aug 27, 2013 at 10:11
  • 2
    Then you have to manually move the existing data, and then copy the strings data into its place. Commented Aug 27, 2013 at 10:13
  • Notice how much easier std::string::insert is: en.cppreference.com/w/cpp/string/basic_string/insert . (You would probably use overload #2, 3, or 4 on that page for this.) Commented Aug 27, 2013 at 11:00

2 Answers 2

2

No. Arrays don't have methods to insert things into them.

STL containers have such methods. This is one of the many reasons why they are preferred to raw arrays.

If you still need to work with raw array for some reason, you can write a function which does what you need, using additional variable to perform the copy in it. But the returned result will be a different array, not your original one.

Sign up to request clarification or add additional context in comments.

2 Comments

how do i index different characters of a string?
1

Create a std::string from the char array, use the insert() method on the string, then convert it back to a char array using the c_str() method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.