Char array insertion HELP!

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

  1. TheMiz19

    TheMiz19 New Member

    Joined:
    Sep 28, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice