Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   how to use string.h to check for vowels and consonants (http://www.velocityreviews.com/forums/t459848-how-to-use-string-h-to-check-for-vowels-and-consonants.html)

ivalki 01-16-2007 12:51 AM

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

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.


Daniel T. 01-16-2007 01:51 AM

Re: how to use string.h to check for vowels and consonants
 
"ivalki" <ivalki@gmail.com> wrote:

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

Chris ( Val ) 01-16-2007 03:51 AM

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

Daniel T. wrote:
> "ivalki" <ivalki@gmail.com> wrote:
>
> > 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


ivalki 01-16-2007 05:32 AM

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

Thanks for helping Chris ( Val ) and Daniel T.


Daniel T. 01-16-2007 01:08 PM

Re: how to use string.h to check for vowels and consonants
 
"Chris ( Val )" <chrisval@gmail.com> wrote:
> Daniel T. wrote:
> > "ivalki" <ivalki@gmail.com> wrote:
> >
> > > 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;
}

Chris ( Val ) 01-16-2007 01:47 PM

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

Daniel T. wrote:
> "Chris ( Val )" <chrisval@gmail.com> wrote:
> > Daniel T. wrote:
> > > "ivalki" <ivalki@gmail.com> wrote:
> > >
> > > > 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



All times are GMT. The time now is 10:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.