Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- // Take a list of words and return a list of words
- // that start with a vowel.
- // This function takes a character and returns
- // true if it is a vowel. Otherwise, it returns false.
- int is_vowel(char letter)
- {
- // The character must be one of these.
- string vowels = "AaEeIiOoUu";
- // Go from the beginning to the
- // end of the list of vowels.
- for (size_t i = 0; i < vowels.size(); ++i)
- {
- // If the letter matches that vowel.
- if (letter == vowels[i])
- {
- // The letter is a vowel.
- return true;
- }
- }
- // The letter didn't match any vowel.
- return false;
- }
- // This function takes a list of words and returns a
- // list of the words that start with a vowel.
- vector<string> get_vowel_words(vector<string>& word_list)
- {
- // These are the words that start with vowels.
- vector<string> words_starting_with_vowels;
- // We are testing this word.
- string current_word;
- // This is the first letter of the word.
- char first_letter = ' ';
- // Go from the beginning to the end of the word list.
- for (size_t i = 0; i < word_list.size(); ++i)
- {
- // Test the next word.
- current_word = word_list[i];
- // Get the first letter of the word.
- first_letter = current_word[0];
- // If the first letter is a vowel,
- if (is_vowel(first_letter))
- {
- // Add the word to the new list.
- words_starting_with_vowels.push_back(current_word);
- }
- }
- return words_starting_with_vowels;
- }
- int main()
- {
- // Create a list of words.
- vector<string> words = { "red", "blue", "orange", "yellow" };
- // Get a list of the words that start with a vowel.
- vector<string> vowel_words = get_vowel_words(words);
- // Display the words that start with a vowel.
- for (size_t i = 0; i < vowel_words.size(); ++i)
- {
- cout << vowel_words[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement