我必須讀取字符串"hello world"
並僅使用for循環輸出每個字母的頻率。教員暗示,我需要用兩個迴路,給了我們下面的代碼開始:使用兩個for-loops計算字符串中的字母
int ch, count;
for (ch ='a'; ch <='z'; ch++) {
//count the number of occurrences in a line
//Print the count>0
}
編輯:我想我會死靈這個問題,並張貼我發現在一年前,由於該解決方案事實上這個問題已經得到了不錯的點擊量。
int count;
int value;
for (int i=65; i<91; i++) {
count=0;
for (int j=0; j<S.length; j++) {
value=(int)S[j];
if (value == i) {
count++;
}
}
if (count>0)
System.out.println((char)i+" -- "+count);
}
對於每個字符,遍歷字符串,並增加計數器,如果你看到它。 – sdasdadas 2013-03-20 19:27:04
所以你已經嘗試過什麼都沒有? – 2013-03-20 19:28:45
你可以請張貼你的老師給你的代碼。 – debianplebian 2013-03-20 19:29:03