By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

Why isn't this working correctly?

P: n/a
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;
}
Nov 14 '05 #1
Share this Question
Share on Google+
29 Replies


P: n/a
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.
Nov 14 '05 #2

P: n/a
"interpim" <le***@atrc.navy.mil> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\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[i]\
);}return 0;}
Nov 14 '05 #3

P: n/a
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.
Nov 14 '05 #4

P: n/a
"interpim" <le***@atrc.navy.mil> 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
Nov 14 '05 #5

P: n/a
Martin Dickopp wrote:
"interpim" <le***@atrc.navy.mil> 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.
Nov 14 '05 #6

P: n/a
Kevin Goodsell <us*********************@neverbox.com> writes:
Martin Dickopp wrote:
"interpim" <le***@atrc.navy.mil> 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
Nov 14 '05 #7

P: n/a
Kevin Goodsell wrote:
Martin Dickopp wrote:
"interpim" <le***@atrc.navy.mil> 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 : bi****@eton.powernet.co.uk
"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
Nov 14 '05 #8

P: n/a
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.
Nov 14 '05 #9

P: n/a
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10

P: n/a
CBFalconer wrote:
Fix your linelength. Lines should not exceed 65 characters.


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

Nov 14 '05 #11

P: n/a
Grumble <in*****@kma.eu.org> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The truth is out there, man! Way out there!"
- Professor Ashfield
Nov 14 '05 #12

P: n/a
Grumble <in*****@kma.eu.org> 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.
Nov 14 '05 #13

P: n/a
Joona I Palaste wrote:
Grumble <in*****@kma.eu.org> 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 :-)

Nov 14 '05 #14

P: n/a
"interpim" <le***@atrc.navy.mil> wrote in message news:<ae******************************@localhost.t alkaboutprogramming.com>...

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
Nov 14 '05 #15

P: n/a
Christopher Benson-Manica wrote:
Grumble <in*****@kma.eu.org> 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.
Nov 14 '05 #16

P: n/a
Joona I Palaste wrote:

Grumble <in*****@kma.eu.org> 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
Nov 14 '05 #17

P: n/a
Default User <fi********@boeing.com.invalid> writes:
Joona I Palaste wrote:

Grumble <in*****@kma.eu.org> 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
Nov 14 '05 #18

P: n/a
Ben Pfaff <bl*@cs.stanford.edu> 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.
Nov 14 '05 #19

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Grumble <in*****@kma.eu.org> 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) ks***@mib.org <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)
Nov 14 '05 #20

P: n/a
Kevin Goodsell wrote:
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.


No, it's just that I read that bit about six times without seeing what I was
looking for.

Some day, I hope to... well, never mind. :-)

--
Richard Heathfield : bi****@eton.powernet.co.uk
"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
Nov 14 '05 #21

P: n/a
Richard Heathfield <do******@address.co.uk.invalid> writes:
No, it's just that I read that bit about six times without seeing what I was
looking for.

Some day, I hope to... well, never mind. :-)


Here, let me help.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Nov 14 '05 #22

P: n/a
Ben Pfaff wrote:
Default User <fi********@boeing.com.invalid> writes:

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

RFC 1855 says 65.


I just checked RFC 1855, and it specifies "fewer than 65 characters",
but this is for mail, not news. I didn't actually find any specification
for line length in the sections on news or any other one-to-many
communication. Maybe it's there and I missed it.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #23

P: n/a
Keith Thompson <ks***@mib.org> spoke thus:
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.


Um, yes, 70. I was testing your ability to count characters... ;)

--
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.
Nov 14 '05 #24

P: n/a
Kevin Goodsell <us*********************@neverbox.com> writes:
Ben Pfaff wrote:
Default User <fi********@boeing.com.invalid> writes:

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

RFC 1855 says 65.


I just checked RFC 1855, and it specifies "fewer than 65 characters",
but this is for mail, not news. I didn't actually find any
specification for line length in the sections on news or any other
one-to-many communication. Maybe it's there and I missed it.


The first sentence in section 3.0 "One-to-Many Communication
(Mailing Lists, NetNews)" is "Any time you engage in One-to-Many
communications, all the rules for mail should also apply."
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Nov 14 '05 #25

P: n/a
Ben Pfaff wrote:

Default User <fi********@boeing.com.invalid> writes:

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


RFC 1855 says 65.

Oh well. Mine will stay at 72. My feeling is that people shouldn't nest
quotes over 3 levels deep.

Brian Rodenborn
Nov 14 '05 #26

P: n/a
On Wed, 17 Dec 2003 14:32:36 +0100, in comp.lang.c , Grumble
<in*****@kma.eu.org> wrote:
CBFalconer wrote:
Fix your linelength. Lines should not exceed 65 characters.


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


so that when you're replying to quoted text, the quotes don't
wrap unexpectedly.
>>>>>imagine how quickly nested quotes can cause wrapping even with

quite short lines of text.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #27

P: n/a
On 17 Dec 2003 13:17:11 -0800, in comp.lang.c , Ben Pfaff
<bl*@cs.stanford.edu> wrote:
Kevin Goodsell <us*********************@neverbox.com> writes:
Ben Pfaff wrote:
> Default User <fi********@boeing.com.invalid> writes:
>
>>
>>Mine's at 72, which I had always heard was the prefered usenet length.
> RFC 1855 says 65.


I just checked RFC 1855, and it specifies "fewer than 65 characters",
but this is for mail, not news. I didn't actually find any
specification for line length in the sections on news or any other
one-to-many communication. Maybe it's there and I missed it.


The first sentence in section 3.0 "One-to-Many Communication
(Mailing Lists, NetNews)" is "Any time you engage in One-to-Many
communications, all the rules for mail should also apply."


I'm going to give you all a special Medal of Sadness for
bothering to look all this up...

The bill is in the post.
The rest of the duck will follow by courier.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #28

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On 17 Dec 2003 13:17:11 -0800, in comp.lang.c , Ben Pfaff
<bl*@cs.stanford.edu> wrote:
Kevin Goodsell <us*********************@neverbox.com> writes:
Ben Pfaff wrote:

> Default User <fi********@boeing.com.invalid> writes:
>
>>
>>Mine's at 72, which I had always heard was the prefered usenet length.
> RFC 1855 says 65.

I just checked RFC 1855, and it specifies "fewer than 65 characters",
but this is for mail, not news. I didn't actually find any
specification for line length in the sections on news or any other
one-to-many communication. Maybe it's there and I missed it.


The first sentence in section 3.0 "One-to-Many Communication
(Mailing Lists, NetNews)" is "Any time you engage in One-to-Many
communications, all the rules for mail should also apply."


I'm going to give you all a special Medal of Sadness for
bothering to look all this up...


This is only the umpteenth time it has come on in comp.lang.c.
It's not like I had to think about it again.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
Nov 14 '05 #29

P: n/a
On 17 Dec 2003 14:50:39 -0800, in comp.lang.c , Ben Pfaff
<bl*@cs.stanford.edu> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 17 Dec 2003 13:17:11 -0800, in comp.lang.c , Ben Pfaff
<bl*@cs.stanford.edu> wrote:
>The first sentence in section 3.0 "One-to-Many Communication
>(Mailing Lists, NetNews)" is "Any time you engage in One-to-Many
>communications, all the rules for mail should also apply."


I'm going to give you all a special Medal of Sadness for
bothering to look all this up...


This is only the umpteenth time it has come on in comp.lang.c.
It's not like I had to think about it again.


in which case you get a special award for Remembering Exceptionally
Useless Offtopic Facts.... : -)
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #30

This discussion thread is closed

Replies have been disabled for this discussion.