用c语言编写程序实现统计一个字符串中各元音字母(即A,E,I,O,U)的个数。不区分大小写。 然后将统计的数据按序输出。 输入格式: asdfghjklmnopqrstuvwxyz 输出格式: Please enter a string: 1 0 0 1 1 说明:每个数字宽度为4,右对齐。 输入样例: asdfghjklmnopqrstuvwxyz 输出样例: Please enter a string: 1 0 0 1 1
时间: 2023-06-05 10:06:31 浏览: 1584
请按照以下方式编写程序:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[100];
int cnt[5] = {0}; // 分别统计 AEIOU 出现的次数
int i;
printf("Please enter a string: ");
scanf("%s", str);
for (i = 0; i < strlen(str); i++)
{
switch (tolower(str[i]))
{
case 'a': cnt[0]++; break;
case 'e': cnt[1]++; break;
case 'i': cnt[2]++; break;
case 'o': cnt[3]++; break;
case 'u': cnt[4]++; break;
}
}
printf("%4d %4d %4d %4d %4d\n", cnt[0], cnt[1], cnt[2], cnt[3], cnt[4]);
return 0;
}
```
注意事项:
- `str` 的长度不要超过 99。
- `tolower` 函数可以将大写字母转换为小写字母,避免了大小写造成的统计错误。
- 格式化输出时,数字占用 4 个字符宽度,右对齐。
相关问题
用c语言编写程序实现统计一个字符串中各元音字母(即A,E,I,O,U)的个数。
在C语言中,你可以通过遍历字符串并检查每个字符是否为元音字母来实现这个功能。下面是一个简单的示例程序:
```c
#include <stdio.h>
#include <ctype.h> // 包含用于处理字符类型的功能
int count_vowels(char* str) {
int vowels[5] = {0}; // 初始化一个数组来存储各元音的数量
for(int i = 0; str[i] != '\0'; i++) {
char c = tolower(str[i]); // 转换为小写字母以便统一比较
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowels[tolower(c) - 'a']++; // 根据ASCII码更新对应元音计数
}
}
return vowels;
}
int main() {
char input[100];
printf("请输入一个字符串: ");
fgets(input, sizeof(input), stdin); // 从标准输入读取字符串,记得去除末尾的换行符
input[strcspn(input, "\n")] = '\0'; // 清除换行符
int vowel_counts[count_vowels(input)];
vowel_counts = count_vowels(input);
printf("各元音字母的个数:\n");
for(int i = 0; i < 5; i++) {
if(vowel_counts[i]) {
printf("字母 '%c': %d\n", 'a'+i, vowel_counts[i]);
}
}
return 0;
}
```
用C语言编写一个程序,从用户输入的字符中统计并输出元音字母(a,e,i,o,u)的个数
在C语言中,你可以使用循环结构遍历用户的输入,并利用条件判断来检查每个字符是否是元音字母。以下是一个简单的示例程序:
```c
#include <stdio.h>
#include <ctype.h> // 引入ctype头文件,用于处理字符类型的函数
int main() {
char input[100]; // 声明一个数组来存储用户输入
int vowel_count = 0; // 初始化元音计数器
printf("请输入一些字符: ");
fgets(input, sizeof(input), stdin); // 从标准输入读取用户输入,包括换行符
for (int i = 0; input[i] != '\0'; i++) { // 遍历输入字符串
char ch = tolower(input[i]); // 将字符转为小写,方便比较
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { // 检查是否是元音
vowel_count++; // 如果是元音,计数器加一
}
}
printf("输入字符串中有 %d 个元音。\n", vowel_count); // 输出结果
return 0;
}
```
在这个程序中,我们首先获取用户的输入,然后通过`tolower()`函数将所有字符转换成小写,因为元音的大小写形式不影响我们的统计。接着,我们逐个检查字符是否属于元音,如果是,则增加计数器。最后,显示元音的数量。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)