Deleting element from array

Hello

I've got a question, how can I delete an element from an array?
I've already done research but delete[] ... didn't seem to work.

This is a small 'pseudo' code to explain what I mean and want to achieve:
1
2
3
4
5
6
7
8
9
10
11
int main() {
char text[250] = "Hello!";
char del = "e";

for (int i = 0; i < 7; i++) {
        if (text[i] == del) {
               text[i] = text[i] - del;
               //Need help only with this part, rest is pseudo code!
             }
         }
}


How can I achieve this?

Thanks for reading,
Niely
Array elements cant be deleted use a vector.

EDIT
or a std::string
Last edited on
The way i did it is find the position of the characher that I want to delete, then swap that postion with its next character til the char is at the last pos, or just swap it with the last character. Then delete it by replacng it with null character or recreate the new one with new size with new[] and delete[]
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm>
#include <cstring>

int main()
{
    char text[250] = "Hello!";
    char del = 'e';
    std::remove(text,
                text + strlen(text) + 1, //1 to get trailing 0 as well
                del);
    std::cout << text;
}
↑ This does the same thing LendraDwi suggested under the hood. (Thought probably in more effective way)
↓ And Lowest0ne mage great visual of what happens.
Last edited on
To create the effect of deleting a character, you can move all of the characters to the right of the deleted character one to the left.
1
2
3
4
5
6
7
// before
0 1 2 3 4 5  6 7 ... 256
H e l l o ! \0

// after
0 1 2 3 4  5  6 7 ... 256
H l l o ! \0 \0
Thanks a lot guys!
Worked awesome!

Topic archived. No new replies allowed.