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
When you erase a character, your index i
is still being incremented, which means you are skipping checking characters that appear right after a vowel.
You need to decrement i
when you erase a character:
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u')
{
word.erase(word.begin() + i); // Removing specific character
--i; // make sure to check the next character
}
Here's a demo.
Also, please avoid the following 2 lines:
#include
using namespace std;
They are dangerous and should not be used.