1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Char array insertion HELP!

Discussion in 'Programming' started by TheMiz19, Sep 28, 2007.

  1. TheMiz19

    TheMiz19 New 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
  2. DaWei

    DaWei New Member

    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.

Share This Page