当前位置: 代码迷 >> C语言 >> 求元音字母
  详细解决方案

求元音字母

热度:276   发布时间:2006-10-21 18:26:44.0
以下是引用shuaiye在2006-10-21 17:50:55的发言:
个在我那运行说getch()出错,我把它改成getchar()还是不能用,输入后还是没有反应,这是怎么回事啊,还有能帮忙解释一下getch()和getchar()的作用和区别吗?谢谢了


加上#include <conio.h>

#include <conio.h>
#include "stdio.h"

void main()
{

int count[26]={0};
char *ch;

printf("please input a string:");
scanf("%s",ch);

do
{
count[*ch-'a']++;
}while(*ch++);

printf("a=%d,e=%d,i=%d,o=%d,u=%d",count[0],count[4],count[8],count[14],count[20]);
getch();
}

getch()表示从标准IO设备输入字符,但不回显。getchar()书上写的很明白,你的书上怎么写的?

很无奈,还是不能运行。。。。。
----------------解决方案--------------------------------------------------------
#include <conio.h>
#include <stdio.h>
#include <string.h>
#define N 1000
main()
{

int count[26]={0};

char str[N];

char *ch=str;

printf("please input a string:");

gets(ch);

do
{
count[*ch-'a']++;
}while(*ch++);

printf("a=%d,e=%d,i=%d,o=%d,u=%d",count[0],count[4],count[8],count[14],count[20]);
getch();
}


----------------解决方案--------------------------------------------------------

指针在使用时,都要对其进行初始化,否则要造成潜在的危害.


----------------解决方案--------------------------------------------------------
以下是引用shuaiye在2006-10-21 18:41:52的发言:
#include <conio.h>
#include <stdio.h>
#include <string.h>
#define N 1000
main()
{

int count[26]={0};

char str[N];

char *ch=str;

printf("please input a string:");

gets(ch);

do
{
count[*ch-'a']++;
}while(*ch++);

printf("a=%d,e=%d,i=%d,o=%d,u=%d",count[0],count[4],count[8],count[14],count[20]);
getch();
}

请问count[*ch-'a']++;怎么解释啊?
----------------解决方案--------------------------------------------------------
count[*ch-'a']++;//想法不错,用几个元音与a的位置比较,a:0 e:4 i:8 o:14 u:20 来调整计数器
但数组开销却很大吧 只有五个元音 而26个空间每个空间都可能有动作发生
----------------解决方案--------------------------------------------------------

  相关解决方案