c语言中怎么实现输入26个英文字母出现的次数?

如题所述

第1个回答  2024-11-27
下面示例是使用C语言实现的示例:
#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];
int count[26] = {0};

printf("请输入26个英文字母:");
fgets(str, sizeof(str), stdin);

for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
int index = tolower(str[i]) - 'a';
count[index]++;
}
}

printf("各个字母出现的次数为:\n");
for (int i = 0; i < 26; i++) {
printf("%c: %d\n", 'a' + i, count[i]);
}

return 0;
}

可按照示例进行改写。
相似回答
大家正在搜