为什么这不正常? [英] Why isn't this working correctly?

查看:61
本文介绍了为什么这不正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的C编程书中快速练习,我无法弄清楚为什么它不能正常工作。它有点工作,但我得到了更奇怪的输出。它应该从文本中删除元音。


#include< ctype.h>

#include< stdio.h>


int isvowel(int letter);


int main(无效)

{

int字母;


while((letter = getchar())!= EOF){

if(isalpha(letter)){

if(isvowel(letter))

getchar(letter);

else putchar(letter);

}

else putchar(letter);

}

返回0;

}


int isvowel(int letter)

{

if(letter ==''A''|| letter ==''E''|| letter ==''我'|| letter ==''O''|| letter ==''U''

|| letter ==''a''|| letter ==''e' '|| letter ==''我'||| letter ==''o''|| letter ==''u'')

返回1;

返回0;

}

解决方案

interpim写道:

从我的C编程书中快速练习,我无法弄清楚它为什么不能正常工作。它有点工作,但我得到了更奇怪的输出。它应该从文本中删除元音。


请将您的新闻客户的换行设置为合理的值(大约72左右)。

#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 (信件);


你好像在这里阅读和丢弃一个角色。这不是
似乎符合您的规格。我想你想说如果字母

不是元音,请打印它。如果它是一个元音,你什么也不做。


但是,通过完全删除isalpha

测试可以简化整个过程,离开只有isvowel测试。

其他putchar(字母);
}
其他putchar(字母);
}
返回0;
}

int isvowel(int letter)
{
if(letter ==''A''|| letter ==''E''|| letter ==' '我'||| letter ==''O''|| letter ==''U''
|| letter ==''a''|| letter ==''e''|| letter ==''i''|| letter ==''o''|| letter ==''u'')


这可以通过转换为首先是大写,然后是

只比较大写元音。

返回1;
返回0;
}




-Kevin

-

我的电子邮件地址有效,但会定期更改。

请联系我使用最近发布的地址。


" interpim" <乐*** @ atrc.navy.mil>写道:
&#包括LT;文件ctype.h>
的#include< stdio.h中>

INT isvowel(INT字母);


使用其他名称。以`是'开头的名称后跟一个

小写字母是保留的。

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


读一个角色......

if(isalpha) (信)){


....然后如果是一封信...

if(isvowel(letter))


....而且它是元音......

getchar(字母);


....读另一个角色?


你看到了问题吗?没有必要再读另一个

字符。


此外,元音是字母的子集,所以没有必要打电话

isalpha()在调用isvowel()之前。

else putchar(字母);
}
其他putchar(字母);
}
返回0;
}




-

int main(void){char p [] =" ; ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\

\\\
",* q =" KL BIcNBFr.NKEzjwCIxNJC英寸; INT I =的sizeof p / 2;字符*和strchr(); INT的putchar(\ <登记/>
);而(* q){I + =和strchr(p,* q ++) - 对;如果(I> =(int)的的sizeof p)的异 - =的sizeof p-1;的putchar(p [ i] \

);}返回0;}


Kevin Goodsell写道:

getchar(letter);



你好像在这里阅读和丢弃一个角色。




第二个想法,这应该导致co加速错误。 getchar确实

不接受任何争论。


-Kevin

-

我的电子邮件地址是有效的,但会定期更改。

要联系我,请使用最近发布的地址。


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


"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;}


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.


这篇关于为什么这不正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆