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

I

ivalki

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.
 
D

Daniel T.

ivalki said:
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.
 
C

Chris ( Val )

Daniel said:
ivalki said:
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
 
D

Daniel T.

Chris ( Val ) said:
Daniel said:
ivalki said:
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;
}
 
C

Chris ( Val )

Daniel said:
Chris ( Val ) said:
Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top