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.
My input was: tour
.
Output was: tur
.
But I am expecting the output like tr
for tour
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string word;
getline(cin, word);
transform(word.begin(), word.end(), word.begin(), ::tolower); // Converting uppercase to lowercase
for (int i = 0; i < word.length(); i++)
{
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u')
{
word.erase(word.begin() + i); // Removing specific character
}
}
cout << word << endl;
return 0;
}
How can I do that? Where is the problem in the code?