Yes those are places left by characters that you 'removed'. You will have to replace them with their neighbors.
Yes those are places left by characters that you 'removed'. You will have to replace them with their neighbors.
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?
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.
How would I copy the next valid character to a specific spot?
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!
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.
Good, but you should indent your code properly:
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: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'; }
Code:str.erase(std::remove(str.begin(), str.end(), 'l'), str.end());
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Haven't read the replies.Code:void remove(char* input, char ch) { char* output = input; while (*input) { if (*input != ch) { *(output++) = *input; } ++input; } *output = 0; }
If you did you would have seen that your algorithm is essentially the same as the one nick753 implemented and posted in post #23.Originally Posted by kmdv
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)