Why isn't this working correctly?

Discussion in 'C Programming' started by interpim, Dec 17, 2003.

  1. interpim

    interpim Guest

    Just a quick exercise from my C programming book, that I can't figure out why it isn't working properly. It kinda works but im getting wierd output. It is supposed to remove the vowels from text.

    #include <ctype.h>
    #include <stdio.h>

    int isvowel(int letter);

    int main(void)
    {
    int letter;

    while ((letter = getchar()) != EOF) {
    if (isalpha(letter)) {
    if (isvowel(letter))
    getchar(letter);
    else putchar(letter);
    }
    else putchar(letter);
    }
    return 0;
    }

    int isvowel(int letter)
    {
    if ( letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U'
    || letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u')
    return 1;
    return 0;
    }
    interpim, Dec 17, 2003
    #1
    1. Advertising

  2. interpim

    Ben Pfaff Guest

    "interpim" <> writes:

    > #include <ctype.h>
    > #include <stdio.h>
    >
    > int isvowel(int letter);


    Use a different name. Names that begin with `is' followed by a
    lowercase letter are reserved.

    > int main(void)
    > {
    > int letter;
    >
    > while ((letter = getchar()) != EOF) {


    Read a character...

    > if (isalpha(letter)) {


    ....then if it's a letter...

    > if (isvowel(letter))


    ....and it's a vowel...

    > getchar(letter);


    ....read another character?

    Do you see the problem? There's no need to read another
    character.

    Also, vowels are a subset of letters, so there's no need to call
    isalpha() before calling isvowel().

    > else putchar(letter);
    > }
    > else putchar(letter);
    > }
    > return 0;
    > }


    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Dec 17, 2003
    #2
    1. Advertising

  3. interpim wrote:

    > Just a quick exercise from my C programming book, that I can't figure out why it isn't working properly. It kinda works but im getting wierd output. It is supposed to remove the vowels from text.


    Please set your news client's line wrap to a reasonable value (72 or so).

    >
    > #include <ctype.h>
    > #include <stdio.h>
    >
    > int isvowel(int letter);
    >
    > int main(void)
    > {
    > int letter;
    >
    > while ((letter = getchar()) != EOF) {
    > if (isalpha(letter)) {
    > if (isvowel(letter))
    > getchar(letter);


    You seem to be reading and discarding a character here. This does not
    seem to match your specification. I think you wanted to say "if letter
    is NOT a vowel, print it." If it is a vowel, you'd do nothing.

    The whole thing could be simplified, though, by removing the isalpha
    test completely, leaving only the isvowel test.

    > else putchar(letter);
    > }
    > else putchar(letter);
    > }
    > return 0;
    > }
    >
    > int isvowel(int letter)
    > {
    > if ( letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U'
    > || letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u')


    This could be simplified a bit by converting to uppercase first, then
    comparing only against the uppercase vowels.

    > return 1;
    > return 0;
    > }
    >
    >


    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Dec 17, 2003
    #3
  4. Kevin Goodsell wrote:

    >> getchar(letter);

    >
    >
    > You seem to be reading and discarding a character here.


    On second thought, this should cause a compilation error. getchar does
    not take any arguments.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Dec 17, 2003
    #4
  5. "interpim" <> writes:

    > int isvowel(int letter);


    Others have already answered your question. I would like to add that
    function names that begin with `is' followed by a lowercase letter are
    reserved for the implementation. To be safe, name your function `is_vowel'
    or something similar.

    Martin
    Martin Dickopp, Dec 17, 2003
    #5
  6. Martin Dickopp wrote:

    > "interpim" <> writes:
    >
    >
    >>int isvowel(int letter);

    >
    >
    > Others have already answered your question. I would like to add that
    > function names that begin with `is' followed by a lowercase letter are
    > reserved for the implementation. To be safe, name your function `is_vowel'
    > or something similar.
    >


    I'd have to check to be sure, but I believe these are reserved for
    future library use, not for the implementation's use.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Dec 17, 2003
    #6
  7. interpim

    Ben Pfaff Guest

    Kevin Goodsell <> writes:

    > Martin Dickopp wrote:
    >
    > > "interpim" <> writes:
    > >
    > >>int isvowel(int letter);

    > > Others have already answered your question. I would like to add that
    > > function names that begin with `is' followed by a lowercase letter are
    > > reserved for the implementation. To be safe, name your function `is_vowel'
    > > or something similar.

    >
    > I'd have to check to be sure, but I believe these are reserved for
    > future library use, not for the implementation's use.


    It amounts to the same thing, because in either case programs
    cannot use them. Regardless of whether you use them to call a
    function in the implementation under such a name or to name your
    own function, it's undefined.
    --
    "In My Egotistical Opinion, most people's C programs should be indented six
    feet downward and covered with dirt." -- Blair P. Houghton
    Ben Pfaff, Dec 17, 2003
    #7
  8. Kevin Goodsell wrote:

    > Martin Dickopp wrote:
    >
    >> "interpim" <> writes:
    >>
    >>
    >>>int isvowel(int letter);

    >>
    >>
    >> Others have already answered your question. I would like to add that
    >> function names that begin with `is' followed by a lowercase letter are
    >> reserved for the implementation. To be safe, name your function
    >> `is_vowel' or something similar.
    >>

    >
    > I'd have to check to be sure, but I believe these are reserved for
    > future library use, not for the implementation's use.


    I just checked. You are correct, if 4.13 of the C89 draft is to be believed.
    (I couldn't actually find this information in the C99 standard.)

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Dec 17, 2003
    #8
  9. Richard Heathfield wrote:
    > Kevin Goodsell wrote:
    >
    >>
    >>I'd have to check to be sure, but I believe these are reserved for
    >>future library use, not for the implementation's use.

    >
    >
    > I just checked. You are correct, if 4.13 of the C89 draft is to be believed.
    > (I couldn't actually find this information in the C99 standard.)
    >


    The corresponding section in C99 appears to be 7.26. Strange that you
    couldn't find it, considering the section heading is the same ("Future
    library directions"). Maybe there's something different in the final
    document - I only have drafts.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Dec 17, 2003
    #9
  10. interpim

    CBFalconer Guest

    interpim wrote:
    >
    > Just a quick exercise from my C programming book, that I can't
    > figure out why it isn't working properly. It kinda works but
    > im getting wierd output. It is supposed to remove the vowels
    > from text.
    >
    > #include <ctype.h>
    > #include <stdio.h>
    >
    > int isvowel(int letter);
    >
    > int main(void)
    > {
    > int letter;
    >
    > while ((letter = getchar()) != EOF) {
    > if (isalpha(letter)) {
    > if (isvowel(letter))
    > getchar(letter);
    > else putchar(letter);
    > }
    > else putchar(letter);
    > }
    > return 0;
    > }
    >
    > int isvowel(int letter)
    > {
    > if ( letter == 'A' || letter == 'E' || letter == 'I'

    || letter == 'O' || letter == 'U'
    > || letter == 'a' || letter == 'e' || letter == 'i'

    || letter == 'o' || letter == 'u')
    > return 1;
    > return 0;
    > }


    Fix your linelength. Lines should not exceed 65 characters.

    Ignoring the use of "isvowel" (you should choose another name
    because that is reserved) your logic is unnecessarily contorted.
    Try:

    int main(void)
    {
    int letter;

    while (EOF != (letter = getchar()))
    if (!isvowel(letter) putchar(letter);
    return 0;
    }

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Dec 17, 2003
    #10
  11. interpim

    Grumble Guest

    CBFalconer wrote:
    > Fix your linelength. Lines should not exceed 65 characters.


    Why 65? I thought a standard terminal had 25 lines and 80 columns.
    Grumble, Dec 17, 2003
    #11
  12. Grumble <> scribbled the following:
    > CBFalconer wrote:
    >> Fix your linelength. Lines should not exceed 65 characters.


    > Why 65? I thought a standard terminal had 25 lines and 80 columns.


    It's there to allow for those > marks in quoted messages. But I think
    65 is overkill - 70 (or even 75 for dastardly types) is fine.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The truth is out there, man! Way out there!"
    - Professor Ashfield
    Joona I Palaste, Dec 17, 2003
    #12
  13. Grumble <> spoke thus:

    > Why 65? I thought a standard terminal had 25 lines and 80 columns.


    Been asked by me already - Chuck's rationale (which I agreed with - I
    now wrap at 65) is that doing so prevents one's text from exceeding 80
    columns when it is quoted (and perhaps re-quoted, ad nauseum).

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 17, 2003
    #13
  14. interpim

    Grumble Guest

    Joona I Palaste wrote:

    > Grumble <> scribbled the following:
    >
    >>CBFalconer wrote:
    >>
    >>>Fix your linelength. Lines should not exceed 65 characters.

    >
    >
    >>Why 65? I thought a standard terminal had 25 lines and 80 columns.

    >
    > It's there to allow for those > marks in quoted messages. But I think
    > 65 is overkill - 70 (or even 75 for dastardly types) is fine.


    Doh! I had not thought of that. It makes a lot of sense :)
    Grumble, Dec 17, 2003
    #14
  15. "interpim" <> wrote in message news:<>...
    >
    > int isvowel(int letter)
    > {
    > if ( letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U'
    > || letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u')
    > return 1;
    > return 0;
    > }


    It would be faster to map character classes to an array. Using c99 designators

    static const int ctype_tbl [128] = {
    ['A']=1, ['E']=1, ['I']=1, ['O']=1, //etc for lower case
    };

    static inline int isvowel (int letter)
    {
    return ctype_tbl [letter] == 1;
    }

    Another solution would be switch-case

    static inline int isvowel (int letter)
    {
    switch (letter)
    case 'A': case 'a':
    case 'E': case 'e':
    case 'I': case 'i':
    case 'O': case 'o':
    return 1;
    return 0;
    }

    compiler can do very good job running the switch in O(1).


    stelios
    stelios xanthakis, Dec 17, 2003
    #15
  16. interpim

    Default User Guest

    Joona I Palaste wrote:
    >
    > Grumble <> scribbled the following:
    > > CBFalconer wrote:
    > >> Fix your linelength. Lines should not exceed 65 characters.

    >
    > > Why 65? I thought a standard terminal had 25 lines and 80 columns.

    >
    > It's there to allow for those > marks in quoted messages. But I think
    > 65 is overkill - 70 (or even 75 for dastardly types) is fine.



    Mine's at 72, which I had always heard was the prefered usenet length.



    Brian Rodenborn
    Default User, Dec 17, 2003
    #16
  17. Christopher Benson-Manica wrote:

    > Grumble <> spoke thus:
    >
    >
    >>Why 65? I thought a standard terminal had 25 lines and 80 columns.

    >
    >
    > Been asked by me already - Chuck's rationale (which I agreed with - I
    > now wrap at 65) is that doing so prevents one's text from exceeding 80
    > columns when it is quoted (and perhaps re-quoted, ad nauseum).
    >


    But that would require an awful lot of re-quoting. My line length is
    currently 72, and that seems like it should be sufficient for all but
    the most extreme cases. I could lower it though, if there's really a
    good reason.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Dec 17, 2003
    #17
  18. interpim

    Ben Pfaff Guest

    Default User <> writes:

    > Joona I Palaste wrote:
    > >
    > > Grumble <> scribbled the following:
    > > > CBFalconer wrote:
    > > >> Fix your linelength. Lines should not exceed 65 characters.

    > >
    > > > Why 65? I thought a standard terminal had 25 lines and 80 columns.

    > >
    > > It's there to allow for those > marks in quoted messages. But I think
    > > 65 is overkill - 70 (or even 75 for dastardly types) is fine.

    >
    > Mine's at 72, which I had always heard was the prefered usenet length.


    RFC 1855 says 65.
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Ben Pfaff, Dec 17, 2003
    #18
  19. Ben Pfaff <> spoke thus:

    > RFC 1855 says 65.


    Why am I not surprised to learn that there's an RFC for this? ;)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 17, 2003
    #19
  20. Christopher Benson-Manica <> writes:
    > Grumble <> spoke thus:
    >
    > > Why 65? I thought a standard terminal had 25 lines and 80 columns.

    >
    > Been asked by me already - Chuck's rationale (which I agreed with - I
    > now wrap at 65) is that doing so prevents one's text from exceeding 80
    > columns when it is quoted (and perhaps re-quoted, ad nauseum).


    Well, no, you don't; the second line of that paragraph is 70 columns
    (72 now that I've quoted it). Which, in my opinion, is just fine.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    (Note new e-mail address)
    Keith Thompson, Dec 17, 2003
    #20
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,843
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    17
    Views:
    491
    Puppet_Sock
    Dec 12, 2005
  3. Mufasa
    Replies:
    6
    Views:
    456
    Juan T. Llibre
    Jun 9, 2008
  4. PerlFAQ Server
    Replies:
    0
    Views:
    127
    PerlFAQ Server
    Jan 20, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    92
    PerlFAQ Server
    Mar 5, 2011
Loading...

Share This Page