[Solved] How to delete all elements that has a vowel in an array [C++]


You are getting blanks because you are not actually removing any characters from your array, you are just replacing them will nulls, and then not ignoring the nulls when outputting the contents of the array.

Instead of doing the removal manually, consider using the standard std::remove_if() algorithm, which can move any vowels to the end of the array. And since you are passing the array size by pointer, you can modify its value to indicate the new size of the array minus any moved vowels.

For example:

#include <algorithm>
#include <cctype>

char* eliminarVocales(char* arreglo, int* size)
{
    if (arreglo)
    {
        *size = std::distance(
            arreglo,
            std::remove_if(arreglo, arreglo + *size,
                [](int ch){ ch = std::toupper(ch); return ((ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U')); }
            )
        );
    }
    return arreglo;
}

Then you can use it like this:

void listarElementos(char* arreglo, int size)
{
    for(int i = 0; i < size; ++i)
        std::cout << "[" << i << "] : " << arreglo[i] << std::endl;
}

...

#include <cstring>

int size = 5;
char *arreglo = new char[size];
std::strcpyn(arreglo, "hello", 5);
...
listarElementos(arreglo, size); // shows "hello"
... 
eliminarVocales(arreglo, &size);
...
listarElementos(arreglo, size); // shows "hll"
...
delete[] arreglo;

If you use a std::vector (or std::string) for your character array, you can then use the erase-remove idiom:

#include <algorithm>
#include <vector>

void eliminarVocales(std::vector<char> &arreglo)
{
    arreglo.erase(
        std::remove_if(arreglo.begin(), arreglo.end(),
            [](int ch){ ch = std::toupper(ch); return ((ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U')); }
        ),
        arreglo.end()
    );
}

void listarElementos(const std::vector<char> &arreglo)
{
    for(std::size_t i = 0; i < arreglo.size(); ++i)
        std::cout << "[" << i << "] : " << arreglo[i] << std::endl;
}

...

#include <cstring>

std::vector<char> arreglo(5);
std::strcpyn(arr.data(), "hello", 5);
...
listarElementos(arreglo); // shows "hello"
... 
eliminarVocales(arreglo);
...
listarElementos(arreglo); // shows "hll"
...

1

solved How to delete all elements that has a vowel in an array [C++]