Char array insertion HELP!

TheMiz19's Avatar, Join Date: Sep 2007
Newbie Member
What would be the code, in C++, to insert or delete a char from an array:

for example:

char word[] = {'h','e','l','l','o'};

and say you want to delete 2-3. So you tell it to delete that and you get "heo"

how would you go about doing that or, what would be the code...thanks
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You can't actually delete elements of an array in C/C++. You would need to move the 'o' up to the position of the first 'l'. You then need to know the size of the result. Your particular implementation makes that difficult, as it is not a C string, but merely an array of char. Had you declared it as a C string, it would have a nul terminator. Then, after moving the 'o' up, you could follow it with a terminator.

Your best bet here is to forgo a simple array and use something else. In this case a string class string would work very well. For non-textual material a vector (or similar) from the STL would do the trick. These objects can shrink and grow as you manipulate them.