Exercise: Find the First Vowel in a Vector
Specification: Write a C++ program that uses a do-while loop to find the first English vowel in a hard-coded std::vector of characters. The vector should contain a mixture of vowels and consonants (for example: {'h', 'e', 'l', 'l', 'o'}). The program should print the first vowel found in the vector. If no vowel is found, it should print a message indicating that no vowel was found.
Instructions
You will be writing your code within the provided function find_first_vowel(const std::vector<char>& vec)
. Do not change the function name or its parameter.
Do-While Loop: Utilize a do-while loop to iterate through the characters in the provided std::vector<char>
named vec
.
Vowel Identification: In each iteration, check if the current character is a lowercase English vowel ('a', 'e', 'i', 'o', 'u').
Output:
If a vowel is found, use cout
to display: "Vowel found: "
followed by the vowel.
If no vowel is found in the entire vector, display: "No vowel was found"
.
Examples: Here are some sample vectors and the expected output:
For vector {'f','r','a','n','k'}
, your function should output: Vowel found: a
.
For vector {'F','R','A','N','K'}
, your function should output: No vowel was found
.
For vector {'h','e','l','l','o'}
, your function should output: Vowel found: e
.
For an empty vector {}
, your function should output: No vowel was found
.
For vector {'x','y','z','o'}
, your function should output: Vowel found: o
.
Do Not Add Line Breaks: Please ensure that you do not add std::endl
or '\n'
to your output.
Code Placement: Write your code between the comments //---- WRITE YOUR CODE BELOW THIS LINE----
and //---- WRITE YOUR CODE ABOVE THIS LINE----
.
Notes
Ensure your solution works correctly for all the provided examples and considers the case of an empty vector.
Focus on correctly implementing the do-while loop and the logic for checking each character.
Your solution should be efficient and neatly formatted to maintain readability.
#include "gtest/gtest.h"
#include "helpers/iohelper.h"
#include
#include
using namespace std;
void find_first_vowel(const std::vector& vec);
namespace {
class Evaluate : public ::testing::Test {};
TEST_F(Evaluate, NoVowel1) {
std::vector vec = {'h', 'l', 'l'};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("No vowel was found", stdout) << "There is no vowel in {'h', 'l', 'l'}";
}
TEST_F(Evaluate, NoVowel2) {
std::vector vec = {'h', 'A', 'l'};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("No vowel was found", stdout) << "There is no lowecase vowel in {'h', 'A', 'l'}";
}
TEST_F(Evaluate, NoVowel3) {
std::vector vec = {};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("No vowel was found", stdout) << "There is no vowel in {}";
}
TEST_F(Evaluate, VowelFirst) {
std::vector vec = {'a', 'p', 'p'};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("Vowel found: a", stdout) << "a is the first vowel in {'a', 'p', 'p'}";
}
TEST_F(Evaluate, VowelFirst1) {
std::vector vec = {'a', 'e', 'i', 'o', 'u'};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("Vowel found: a", stdout) << "a is the first vowel in {'a', 'p', 'p'}";
}
TEST_F(Evaluate, VowelLast) {
std::vector vec = {'p', 'x', 'Y', 'o'};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("Vowel found: o", stdout) << "o is the first vowel in {'p', 'x', 'Y', 'o'}";
}
TEST_F(Evaluate, VowelMiddle) {
std::vector vec = {'s', 'm', 'a', 'r', 't'};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("Vowel found: a", stdout) << "a is the first vowel in {'s', 'm', 'a', 'r', 't'}";
}
TEST_F(Evaluate, VowelMiddle1) {
std::vector vec = {'h', 'e', 'l', 'l', 'o'};
std::pair output = CAPTURE_OUTPUT(find_first_vowel(vec));
std::string stdout = output.first;
EXPECT_EQ("Vowel found: e", stdout) << "a is the first vowel in {'h', 'e', 'l', 'l', 'o'}";
}
}
#include
#include
using namespace std;
/*******************************************************
* Write your code in the provided area.
*
* This function should use a do while loop to iterate over
* the automatically provided vector of characters looking
* for the first occurrence of a lowercase English vowel (a,e,i,o,u).
*
* If a vowel is found, you should display to cout:
*
* "Vowel found: " followed by the vowel that was found.
*
* If no vowel is found in the vector, then you should display to cout:
*
* "No vowel was found"
*
* For example, below are several examples of vectors and what your output should be:
*
* {'f','r','a','n','k'} Vowel found: a
* {'F','R','A','N','K'} No vowel was found
* {'h','e','l','l','o'} Vowel found: e
* {} No vowel was found
* {'x','y','z','o'} Vowel found: o
*
* Please do NOT add std::endl or '\n' to your output statement.
* *****************************************************/
void find_first_vowel(const std::vector& vec) {
//---- WRITE YOUR CODE BELOW THIS LINE----
size_t i{0};
bool vowel_found {false};
if (!vec.empty()) {
do {
if (vec.at(i) == 'a' || vec.at(i) == 'e' || vec.at(i) == 'i' || vec.at(i) == 'o' || vec.at(i) == 'u') {
vowel_found = true;
} else {
i++;
}
} while (!vowel_found && i < vec.size());
}
if (vowel_found) {
cout << "Vowel found: " << vec.at(i);
} else {
cout << "No vowel was found";
}
//---- WRITE YOUR CODE ABOVE THIS LINE----
}