"Chris ( Val )" <ch******@gmail.comwrote:
Daniel T. wrote:
"ivalki" <iv****@gmail.comwrote:
How do i use the functions found in string.h to check if a letter is
vowel and a consonant ?
Why are you using those functions? I suggest you use algorithm instead.
#include <algorithm>
bool is_vowel( char c )
{
const char v[11] = "aeiouAEIOU";
return std::find( v, v + 11, c ) != v + 11;
}
[ snip ]
I would only use an algorithm if I really needed to, and
the use of magic numbers don't really help either :)
bool is_vowel( char c, std::string vowels = "aeiouAEIOU" ) {
return vowels.find_first_of( c ) != std::string::npos;
}
I thought an implementation like that too, but was somewhat concerned
about using a function that creates a string every call in a function
that would likely be used in an inner loop.
Also, your signature allows things like:
if ( is_vowel( c, "!@#$%^&*()" ) )...
Which is, at best, confusing. For your particular implementation, I
suggest a name change to something like "is_one_of" and dump the default
parameter.
Otherwise, I agree with you.
How about a compromise:
bool is_vowel( char c ) {
static const std::string vowels = "aeiouAEIOU";
return vowels.find( c ) != std::string::npos;
}