I am learning C++. Today I have written a code to remove vowels form a string. It works fine in some tests. But this test is failing to remove "u" from a string. M
After erase
, i++
is performed. That means one element is bypassed by the check.
Change the for
loop to
for (int i = 0; i < word.length(); )
{
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u')
{
word.erase(word.begin() + i); // Removing specific character
}
else
{
i++; // increment when no erasion is performed
}
}