Write a C code to ask the user to enter an English word and count the occurrence of each
letter (ignore the case). For example, if the user enters the word ‘Tomorrow’ then your code
should output the following result:
T(1), O(3), M(1), R(2), W(1)
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c = 0, count[26] = {0}, x;
int index = 1, idx[26] = {0};
printf("Enter a string\n");
gets(string);
while (string[c] != '\0') {
/** Considering characters from 'a' to 'z' only and ignoring others. */
if (string[c] >= 'a' && string[c] <= 'z') {
x = string[c] - 'a';
count[x]++;
}
if (string[c] >= 'A' && string[c] <= 'Z') {
x = string[c] - 'A';
count[x]++;
}
if(idx[x]==0) {idx[x]=index++;}
c++;
}
for (int i=1;i<index;i++) {
for (int j=0;j<26;j++) {
if (idx[j]==i) {
if(i>1){printf(",");}
printf("%c(%d)", j + 'A', count[j]);
break;
}
}
}
return 0;
}
假設您不是伸手牌,只是看不懂英文...
幫您翻譯題目:
編寫C代碼,讓用戶輸入英文單詞並計算每個字母的出現次數(忽略大小寫)。
例如,如果用戶輸入單詞'Tomorrow',
那麼您的代碼應輸出以下結果:
T(1), O(3), M(1), R(2), W(1)
答案請參考海綿寶寶~~