How do I remove a character from character array?

This is a discussion on How do I remove a character from character array? within the C++ Programming forums, part of the General Programming Boards category; Yes those are places left by characters that you 'removed'. You will have to replace them with their neighbors....

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Yes those are places left by characters that you 'removed'. You will have to replace them with their neighbors.

  2. #17
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    By replacing them with their neighbors you mean pushing back all the characters after the space that were left? And I would do this with the 2nd char array?

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Yes that is exactly what I mean. If you know exactly where you want to copy the next valid character though, then that would avoid the problem.

  4. #19
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    How would I copy the next valid character to a specific spot?

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Keep track of the spot.

  6. #21
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I just can't get it, how can I push back all of the characters after the space of the removed characters? I tried finding it in my book, and there is nothing like that. I tried looking online and can't find anything that relates to this. I've never done this before, I'm stuck!

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nick753 View Post
    I've never done this before, I'm stuck!
    1) This is a bad attitude to have: at every point in your career, you will almost always be doing something you haven't done before. (If you had done it before, you'd just reuse the code and there you are.)

    2) Have you read any posts in the thread other than your own? We've been pretty explicit on how to do this. You know where to start replacing, because that's where you found your letter. Every character after that, you need to move forward one space. So, do that.

  8. #23
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by tabstop View Post
    1) This is a bad attitude to have: at every point in your career, you will almost always be doing something you haven't done before. (If you had done it before, you'd just reuse the code and there you are.)

    2) Have you read any posts in the thread other than your own? We've been pretty explicit on how to do this. You know where to start replacing, because that's where you found your letter. Every character after that, you need to move forward one space. So, do that.
    I think my version is much easier, thanks for trying to help though!

    Code:
    void remove(char cstring[], char letter)
    {
    int j = 0;
    int i ;
    
    for( i = 0; cstring[i] != '\0'; i++ )
    {
    cstring[j] = cstring[i];
    
    
    if(cstring[i] != letter)
    ++j;	
    
    }	
    // terminate j
    cstring[j] = '\0';
    }

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Good, but you should indent your code properly:
    Code:
    void remove(char cstring[], char letter)
    {
        int j = 0;
    
        for (int i = 0; cstring[i] != '\0'; ++i)
        {
            cstring[j] = cstring[i];
    
            if (cstring[i] != letter)
                ++j;
        }
    
        // terminate j
        cstring[j] = '\0';
    }
    Since i is only in the scope of the for loop, I have moved it to be local to the for loop. Finally, if you actually had to do this other than in an exercise, an appropriate solution would be to use a std::string, and then write:
    Code:
    str.erase(std::remove(str.begin(), str.end(), 'l'), str.end());
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Code:
    void remove(char* input, char ch)
    {
        char* output = input;
        while (*input)
        {
            if (*input != ch)
            {
                *(output++) = *input;
            }
            ++input;
        }
        *output = 0;
    }
    Haven't read the replies.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by kmdv
    Haven't read the replies.
    If you did you would have seen that your algorithm is essentially the same as the one nick753 implemented and posted in post #23.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character array initialization problem
    By ThatDudeMan in forum C Programming
    Replies: 7
    Last Post: 12-03-2010, 02:56 PM
  2. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  3. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  4. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  5. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21