编写一个c++版的统计多个字符串中哪个字母出现次数最多的
时间: 2023-03-20 13:00:28 浏览: 101
以下是用C语言编写统计多个字符串中哪个字母出现次数最多的程序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STR_LEN 1000
int main() {
char str[MAX_STR_LEN], c;
int count[26] = {0}; // 用于存储每个字母出现次数的数组,初始化为0
int i, j, max_count;
printf("请输入字符串(以#结束):\n");
// 逐个读入字符,并统计每个字母出现的次数
while ((c = getchar()) != '#') {
if (c >= 'a' && c <= 'z') {
count[c-'a']++;
} else if (c >= 'A' && c <= 'Z') {
count[c-'A']++;
}
}
// 查找出现次数最多的字母
max_count = count[0];
for (i = 1; i < 26; i++) {
if (count[i] > max_count) {
max_count = count[i];
}
}
// 输出出现次数最多的字母
printf("出现次数最多的字母为:");
for (i = 0; i < 26; i++) {
if (count[i] == max_count) {
printf("%c ", 'a'+i);
}
}
printf("\n");
return 0;
}
```
该程序使用了一个长度为26的数组`count`来存储每个字母出现的次数,其中`count[0]`表示字母'a'出现的次数,`count[1]`表示字母'b'出现的次数,以此类推。在读入每个字符时,判断其是否为字母,如果是,则将对应的`count`数组元素加1。最后,遍历`count`数组,找到出现次数最多的字母,并输出该字母。如果有多个字母出现次数相同且最多,则一并输出。