輸入範例:abb cccd eefff
輸出範例:a=1,b=2,c=3,d=1,e=2,f=3
cnt[26] = {0}。getchar 或 fgets),若為字母 (isalpha),轉小寫 (tolower) 後對應索引遞增。#include <stdio.h>
#include <ctype.h>
int main()
{
int cnt[26] = {0};
int ch;
while ((ch = getchar()) != EOF) {
if (isalpha(ch)) {
cnt[tolower(ch) - 'a']++;
}
}
int first = 1;
for (int i = 0; i < 26; i++) {
if (cnt[i] > 0) {
if (!first) putchar(',');
printf("%c=%d", 'a' + i, cnt[i]);
first = 0;
}
}
putchar('\n');
return 0;
}
$ ./a.out abb cccd eefff a=1,b=2,c=3,d=1,e=2,f=3