Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why isn't this working correctly?

Reply
Thread Tools

Why isn't this working correctly?

 
 
interpim
Guest
Posts: n/a
 
      12-17-2003
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;
}


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      12-17-2003
"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[]="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;}
 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      12-17-2003
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.
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      12-17-2003
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.
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      12-17-2003
"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
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      12-17-2003
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.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-17-2003
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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-17-2003
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
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      12-17-2003
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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-17-2003
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!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57