0

## 求解 拜託各位

LQe 2019-04-09 21:00:101616 瀏覽

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)

froce iT邦大師 1 級 ‧ 2019-04-09 22:00:23 檢舉
ccutmis iT邦高手 6 級 ‧ 2019-04-09 22:22:35 檢舉
>>Tomorrow
>>T(1),O(3),R(2),W(1)

Give Up
G(1) I(1) V(1) E(1) U(1) P(1)

### 2 個回答

4

iT邦大神 1 級 ‧ 2019-04-09 22:43:05

``````    #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;
}
``````

0

iT邦高手 1 級 ‧ 2019-04-10 10:26:26

T(1), O(3), M(1), R(2), W(1)

Chrome還可以幫忙翻譯網頁...

ccutmis iT邦高手 6 級 ‧ 2019-04-10 16:06:35 檢舉

chwei_wu iT邦新手 4 級 ‧ 2019-04-11 16:11:48 檢舉

...重點是...發問者沒給海綿寶寶最佳解答~~~