You should be aware that in terms of naming, this doesn't make sense:
Code:
for (char vowelsList : vowels) {
vowels forms the range that you're iterating over, which means that vowelsList is a vowel, not a list of vowels, so you should have named it vowel.
Instead of directly looping over the vowels, I would have stored them in a string that could be searched:
Code:
string getVowels(const string& statement) {
const string vowels = "aeiou";
string result;
for (char c : statement) {
if (vowels.find(c) != string::npos) {
result += c;
}
}
return result;
}
Notice that I have made statement a const reference since you don't need a copy of the statement from the caller, and you don't need move semantics.
If you were feeling more adventurous, you could #include <algorithm> and replace even that remaining loop:
Code:
copy_if(statement.begin(), statement.end(), back_inserter(result),
[&vowels](char c) { return vowels.find(c) != string::npos; });
EDIT:
There is yet another way, but it is fundamentally different from your approach, but perhaps it is good to keep in mind. What you did, and what I showed you, are variations on the theme of "copy vowels from the source to the result". This other way is "remove non-vowels from a copy of the source to become the result":
Code:
string getVowels(string statement) {
const string vowels = "aeiou";
statement.erase(
remove_if(statement.begin(), statement.end(),
[&vowels](char c) { return vowels.find(c) == string::npos; }),
statement.end()
);
return statement;
}
You would see that I have changed the function parameter back to string so that a copy is made, and then use the remove_if algorithm to "remove" the characters of statement in-place such that all the non-vowels are at the end. Then, I call statement's erase member function to erase the "removed" characters, leaving the vowels that are then returned.