c语言编程入门教程_c语言编程
统计出一行字符串中各元音字母的个数(大小写不区分)。提示:首先从键盘接收一行字符串存放在字数组中,然后逐个字符判断是否是元音字母,如果是,则数量加1
最佳答案
答题时本以为很简单,结果折腾了半天gdb才想起来要用gets( )接收整句。哎,有段时间不用,真是脑才了。另外,C语言真是细节决定一切啊!
下面是我在Windows 10中运行程序的截图:

程序结构没有太多设计,仅仅是完成功能而已,供题主一笑吧:
---------------------------------------------------------------------------
/**
* Author : grass_rt
* File name: statistics.c
*/
#include <stdio.h>
#define BUF_SZ (50) // 输入字符串的长度
#define VOWEL_LETTERS (5) // 元音字母的数量
enum type_vowels {A=0, E, I, O, U};
/**
* void test_string(const char* str, int frq[]);
* 作用: 检查字符串str中原因字母出现的次数,并保证在int frq[]中。
* 输入参数:
* 1. const char* str: 输入的字符串。
* 2. int frq[] : 记录元音字母出现频率的数组。
* 输出参数: 无
*/
void test_string(const char* str, int frq[]);
/**
* void print_frq(int frq[]);
* 作用: 打印各元音字母的出现频率
* 输入参数:
* 1. int frq[]: 记录元音字母出现频率的int数组。
* 输出参数: 无
*/
void print_frq(int frq[]);
int main(void)
{
char input[BUF_SZ];
int frq[VOWEL_LETTERS] = {0, };
printf("Please input a string: ");
gets(input);
test_string(input, frq);
print_frq(frq);
return 0;
}
void test_string(const char* str, int frq[])
{
int i;
for (i = 0; str[i]; ++i)
{
switch(str[i])
{
case 'A':
case 'a':
++frq[A]; break;
case 'E':
case 'e':
++frq[E]; break;
case 'I':
case 'i':
++frq[I]; break;
case 'O':
case 'o':
++frq[O]; break;
case 'U':
case 'u':
++frq[U];
}
}
}
void print_frq(int frq[])
{
printf("Frq of A: %d\n", frq[A]);
printf("Frq of E: %d\n", frq[E]);
printf("Frq of I: %d\n", frq[I]);
printf("Frq of O: %d\n", frq[O]);
printf("Frq of U: %d\n", frq[U]);
}
其他回答
其它网友回答:
。。。这个你是要代码吗? scanf读入 strlen判断长度 for循环遍历 五个if判断