Thread: How do I remove a character from character array?

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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,336
    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
    28,413
    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());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    733
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 03: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, 06:03 PM