求句子中元音字母数
代码#include <stdio.h>
int main()
{
int counta,counte,counti,counto,countu;
int ch,sum=0;
printf("请输入一个英文句子:\n");
while(getchar()!='\n')
{
ch=getchar();
switch(ch)
{
case 'a':counta++;
case 'A':counta++;
case 'e':counte++;
case 'E':counte++;
case 'i':counti++;
case 'I':counti++;
case 'o':counto++;
case 'O':counto++;
case 'u':countu++;
case 'U':countu++;
}
}
if(counta>=1)
{
sum++;
}
if(counte>=1)
{
sum++;
}
if(counti>=1)
{
sum++;
}
if(counto>=1)
{
sum++;
}
if(countu>=1)
{
sum++;
}
printf("您输入的句子中,包含元音字母%d个!\n",sum);
printf("其中:a(%d),e(%d),i(%d),o(%d),u(%d)",counta,counte,counti,counto,countu);
return 0;
}
运行结果......
请输入一个英文句子:
aeiou
您输入的句子中,包含元音字母4个!
其中:a(0),e(3),i(2),o(61),u(4)
--------------------------------
Process exited after 11.87 seconds with return value 0
请按任意键继续. . .
程序目的:用户输入一行英文句子,当用户按下回车时,结束运算,输出结果,要求一:计算句子所含元音字母种类数(如aeio算4种,aaeio也是四种)要求二:计算句子中所含每种元音字母的个数(如输入aeiio,期待结果为a(1),e(1),i(2),o(1),u(0))
结果1为什么错得这么离谱啊,求助要求二 没有初始化,没有break;,while()里getchar()每次吃掉一个字符 改成这样:
#include <stdio.h>
int main()
{
int counta = 0,counte = 0,counti = 0,counto = 0,countu = 0;
int ch,sum=0;
printf("请输入一个英文句子:\n");
while((ch = getchar())!='\n')
{
switch(ch)
{
case 'a':counta++;break;
case 'A':counta++;break;
case 'e':counte++;break;
case 'E':counte++;break;
case 'i':counti++;break;
case 'I':counti++;break;
case 'o':counto++;break;
case 'O':counto++;break;
case 'u':countu++;break;
case 'U':countu++;break;
}
}
if(counta>=1)
{
sum++;
}
if(counte>=1)
{
sum++;
}
if(counti>=1)
{
sum++;
}
if(counto>=1)
{
sum++;
}
if(countu>=1)
{
sum++;
}
printf("您输入的句子中,包含元音字母%d个!\n",sum);
printf("其中:a(%d),e(%d),i(%d),o(%d),u(%d)",counta,counte,counti,counto,countu);
return 0;
}
#include <stdio.h>
int main()
{
int counta = 0,counte = 0,counti = 0,counto = 0,countu = 0;//初始化
int ch,sum=0;
printf("请输入一个英文句子:\n");
while((ch = getchar())!='\n')
{
switch(ch)
{
case 'a':counta++;break;//break退出
case 'A':counta++;break;
case 'e':counte++;break;
case 'E':counte++;break;
case 'i':counti++;break;
case 'I':counti++;break;
case 'o':counto++;break;
case 'O':counto++;break;
case 'u':countu++;break;
case 'U':countu++;break;
}
}
if(counta>=1)
{
sum++;
}
if(counte>=1)
{
sum++;
}
if(counti>=1)
{
sum++;
}
if(counto>=1)
{
sum++;
}
if(countu>=1)
{
sum++;
}
printf("您输入的句子中,包含元音字母%d个!\n",sum);
printf("其中:a(%d),e(%d),i(%d),o(%d),u(%d)",counta,counte,counti,counto,countu);
return 0;
} baige 发表于 2020-8-6 10:41
没有初始化,没有break;,while()里getchar()每次吃掉一个字符
请输入一个英文句子:
aeiou
您输入的句子中,包含元音字母5个!
其中:a(2),e(4),i(6),o(8),u(10)
--------------------------------
Process exited after 4.092 seconds with return value 0
请按任意键继续. . .
int counta=0,counte=0,counti=0,counto=0,countu=0;
while(ch!='\n')
这怎么还递增了啊 122Ml 发表于 2020-8-6 10:46
你看一眼我的回复 qiuyouzhi 发表于 2020-8-6 10:47
你看一眼我的回复
ok,我没写break....... #include <stdio.h>
int main()
{
int counta = 0, counte = 0, counti = 0, counto = 0, countu = 0, sum;
char ch;
printf("请输入一个英文句子:\n");
while ((ch=getchar()) != '\n')
{
switch (ch)
{
case 'a':
case 'A':
counta++;
break;
case 'e':
case 'E':
counte++;
break;
case 'i':
case 'I':
counti++;
break;
case 'o':
case 'O':
counto++;
break;
case 'u':
case 'U':
countu++;
break;
}
}
sum = counta + counte + counti + counto + countu;
printf("您输入的句子中,包含元音字母%d个!\n其中:a(%d),e(%d),i(%d),o(%d),u(%d)", sum, counta, counte, counti, counto, countu);
return 0;
}
页:
[1]