Thread: How to delete something from the array?

  1. #1
    Registered User alex6852's Avatar
    Join Date
    Sep 2001
    Posts
    43

    Question How to delete something from the array?

    I have one tiny problem(ones more) that preventing me from finishing my project.
    I have big array(buffer to be exact) that holds portion of data. I want to delete few spaces ( '_ _ _' ) from it. But how can I delete something from the middle of my array and move data so it will close the gap??? Let's make small example:

    char buffer[20] = "I like*****C++";

    How can I erase those stars '*' so I would get "I like C++" ???????
    Please help.
    C++ rulez!!!

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Do you have to delete them? I would suggest you write to a temporary array skipping over the '*', then copy the temporary string back into the original.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    with an array it is harder to do this because you have to resize. It might be easier if you used a linked list.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    if it's just a string, using a linked list for that is hopelessly redundant... [tho for any other conglomerate data type, that's your ace...]

    either way, if you reallocated a new buffer [calculate it's size by counting unwanted spaces beforehand...] you can do it... or you can do it by looping through the original array... reording it as you go along... and reallocating it smaller at the end... both work...
    hasafraggin shizigishin oppashigger...

  5. #5
    Registered User alex6852's Avatar
    Join Date
    Sep 2001
    Posts
    43
    O.k. let me explain what I'm doing. I'm working on the file archiver that would do following steps:

    1) Read peace of file in the buffer ( this array I'm talking about)
    2) Then it would change array(buffer) in the way so it would say how many spaces been erased there, so I can restore it back to it's original state.

    Example:

    text " I _ _ _ _ like _ _ _ _ _ _ _ _ _ C++" ( '_' is space)

    would look like " I /4\like/9\C++ " in the output file. This methood will "compress" any file a littel bit (espeshily if it has a lot of spaces in it).

    3) And after that it would write buffer to the output file.

    Here's the problem... Through lines and lines of complicated code I got to the point where my array looks like this:

    " I /4\ _ like /9\ _ _ _ _ _ _ C++ ". Now I need to get rid of
    those spaces ( _ )

    I hope that makes some sence. Please help.
    C++ rulez!!!

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    Lightbulb

    i agree with DoubleAnti that linkedlist would really be redundant...in my opinion the easiest way to erase an item from an array would be to simply shift it over....

    for instance and array of 15 characters (written below), you said that you wanted to erase the stars....

    "I love **** C++"

    First you find out the size of an array, an in this case it is 16, actually 15 if we won't include the terminating character... then you find (let's say) the last (fourth) star whose index in the array would be Array[11].... the next step you would perform is to shift everything else that's beyond "index" 11 back....

    for example:

    you define int Position to 11
    you make the next character instead of actual Array[12] index 11
    (Array[Position] = Array[Position +1] )... then you do the same thing with (like i said) remaining character...afer you have shifted all the rest of characters by one position back, you will have one star erased and will have a gap at the end...so what you do is make the Size of an array smaller by 1 (Size= Size - 1)...
    then if you want to do the same thing to the rest of the stars you repeat this step with now defining your int Position smaller by one
    (Position = Position -1) in this case Position now will equal to 10...
    and like i just said you repeat the same process.....

    you could use a simple iterative function or recursion.....

    i hope that helps.....

    let us know..

    Regards,
    matheo917

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    hummm, i guess a linked list would be redundent, sorry.

    i'd probably just shift it over too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. delete array of arrays
    By tmod in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2007, 03:37 PM
  3. Delete duplicate items in an array
    By chaos in forum C Programming
    Replies: 4
    Last Post: 06-16-2005, 02:51 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM