统计字符串中每个字母的个数 C语言

时间:2025-03-18 09:21:48
int main() { char str[100]; int count[26] = { 0 }, i = 0; printf("请输入字符串: \n"); gets(str); while (str[i]!='\0') { if (str[i] >= 'a' && str[i] <= 'z') // 对小写字母的处理 count[str[i] - 'a']++; else if (str[i] >= 'A' && str[i] <= 'Z') // 对大写字母的处理 count[str[i] - 'A']++; i++; } printf("统计结果为:\n"); for (i = 0; i < 26; i++) { if (count[i] != 0) { printf("%c - %d\n", i + 'A', count[i]); } } return 0; }