Updated: June 19th, 2023
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<char> letters;
string str1;
getline(cin, str1);
char vowels[5] = {'a', 'e', 'i', 'o', 'u'};
int vowelsCount = 0;
for (size_t i = 0; i < str1.length(); ++i) {
char currentChar = str1[i];
letters.push_back(currentChar);
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < letters.size(); j++) {
if (vowels[i] == letters[j]) {
vowelsCount++;
}
}
}
cout << "Number of vowels found: " << vowelsCount << endl;
return 0;
}
At first, I made some variables, arrays, and vectors. And I then used getLine to receive the data from the user. Since we need to have vowels, I made an array including the vowels. After that, I made a for loop that push_back the individual letters into the vector for comparison with the vowels.
Using nested for loops, I could compare the vowels and the letters. Using vowelsCount++, it will count the vowels.
As a result, we can see the number of the vowels in a string or a name.