Given an array of words, and I want to find all the “Hebrew” words. “Hebrew” words, in our definition, are words without any vowels. For example: “xyzzy” could be considered a Hebrew word, while “ball” is not. The only vowels in English, in case you don’t remember, are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. Assume all the words in the array consist only of lowercase letters (i.e. no uppercase letters, no spaces, no other symbols).
a) Write the function isHebrew that takes in a word (of type string) that consists only of lowercase letters and returns true has the “Hebrew” nature, false otherwise.
bool isHebrew(string word)
{
b) Write the function hebrew that takes in an array of words (array of strings), keeps only candidate “Hebrew” words in the array, and returns the new size of the array. For example:
string words[4] = {“xyz”, “ucla”, “sky”, “this”};
int newSize = hebrew(words);
// newSize should be 2, and words should be {“xyz”, “sky”} or {“sky”, “xyz”}.
// words will have two more elements, but we do not care what values they carry.
// The first two elements are what matter.
Let n be the size of the array. Return -1 if n is negative. int hebrew(string words[], int n)
{
Could you please help me this my practice test from midterm exam and I ahve no idea how to do it and if you could help me understand :)
#include <iostream>
#include <string>
#include <algorithm>
#include <type_traits>
#include <iterator>
size_t hebrew( std::string a[], size_t n )
{
constchar *vowels = "aeiou";
auto isHebrew = [=]( const std::string &s )
{
return ( s.find_first_of( vowels ) == std::string::npos );
};
std::stable_partition( a, a + n, isHebrew );
return ( std::distance( a, std::partition_point( a, a + n, isHebrew ) ) );
}
int main()
{
std::string a[] = { "xyz", "ucla", "sky", "this" };
// for each ( const auto &s in a ) std::cout << s << ' '; // MS VC++ 2010
for ( constauto &s : a ) std::cout << s << ' ';
std::cout << std::endl;
size_t n = hebrew( a, std::extent<decltype( a )>::value );
std::cout << "Number of hebrew words is " << n << std::endl;
// for each ( const auto &s in a ) std::cout << s << ' '; // MS VC++ 2010
for ( constauto &s : a ) std::cout << s << ' ';
std::cout << std::endl;
}
Also instead of algorithms that deal with partitions you can use algorithm std::remove_if depending on whether you need to keep non-hebrew words in the array.