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
Bytes IT Community
+ 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<endoffile)
{

read(file,&char_read,1);

if (strpbrk(vowel,char_read)!=NULL)
{
printf("\n vowel ");

}
else if (strpbrk(consonant,char_read)!=NULL)
{
printf("\n consonant ");

}
printf(" %c",char_read);

pozitia_curenta++;
}

}

and somehow the strpbrk does not work .. i also tried with several
functions but i haven't got this right so far

thanks
Vali.

Jan 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"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;
}
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<endoffile)
{

read(file,&char_read,1);
if (strpbrk(vowel,char_read)!=NULL)
Probably broke because of the missing null.
Jan 16 '07 #2

P: n/a

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;
}

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 )" <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;
}
Jan 16 '07 #5

P: n/a

Daniel T. wrote:
"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;
}
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.