加上#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();
}
----------------解决方案--------------------------------------------------------
指针在使用时,都要对其进行初始化,否则要造成潜在的危害.
----------------解决方案--------------------------------------------------------
#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个空间每个空间都可能有动作发生
----------------解决方案--------------------------------------------------------