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

Discussion in 'C++' started by ivalki, Jan 16, 2007.

  1. ivalki

    ivalki Guest

    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.
     
    ivalki, Jan 16, 2007
    #1
    1. Advertising

  2. ivalki

    Daniel T. Guest

    "ivalki" <> 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.
     
    Daniel T., Jan 16, 2007
    #2
    1. Advertising

  3. Daniel T. wrote:
    > "ivalki" <> 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
     
    Chris ( Val ), Jan 16, 2007
    #3
  4. ivalki

    ivalki Guest

    Thanks for helping Chris ( Val ) and Daniel T.
     
    ivalki, Jan 16, 2007
    #4
  5. ivalki

    Daniel T. Guest

    "Chris ( Val )" <> wrote:
    > Daniel T. wrote:
    > > "ivalki" <> 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;
    }
     
    Daniel T., Jan 16, 2007
    #5
  6. Daniel T. wrote:
    > "Chris ( Val )" <> wrote:
    > > Daniel T. wrote:
    > > > "ivalki" <> 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
     
    Chris ( Val ), Jan 16, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Slater
    Replies:
    8
    Views:
    1,114
    Howard
    May 17, 2004
  2. Matt
    Replies:
    7
    Views:
    1,663
    Oliver Wong
    Jan 2, 2007
  3. Replies:
    5
    Views:
    978
    X-Centric
    Jun 30, 2005
  4. Dondi
    Replies:
    9
    Views:
    379
    Corey Haines
    Feb 2, 2008
  5. nwaits
    Replies:
    10
    Views:
    239
Loading...

Share This Page