By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 448,652 Members | 1,727 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

how to use string.h to check for vowels and consonants

 P: n/a Hi How do i use the functions found in string.h to check if a letter is vowel and a consonant ? I have this code : ......... const char vowel[5]="aeiou"; const char consonant[21]="bcdfghjklmnpqrstvwxyz"; while (position
Share this Question
5 Replies

 P: n/a "ivalki" bool is_vowel( char c ) { const char v[11] = "aeiouAEIOU"; return std::find( v, v + 11, c ) != v + 11; } I have this code : ........ const char vowel[5]="aeiou"; const char consonant[21]="bcdfghjklmnpqrstvwxyz"; The above two arrays are missing nulls at the end, and they are missing the upper case versions of the letters. while (position

 P: n/a Daniel T. wrote: "ivalki" 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; } Cheers, Chris Val Jan 16 '07 #3

 P: n/a Thanks for helping Chris ( Val ) and Daniel T. Jan 16 '07 #4

 P: n/a "Chris ( Val )" 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; } Jan 16 '07 #5

 P: n/a Daniel T. wrote: "Chris ( Val )" 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 > 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; } In fact, I was going to offer such an alternative right after I posted, but was at work and had no time left to post back. Having said that..., I agree with you're comments in full! :) Cheers, Chris Val Jan 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.