@邁克爾,我看見你回答我張貼之前,我只是想重複一個更詳細的解答。如果您編輯自己以添加更多解釋,那麼我會刪除我的。我沒有直接編輯你的,因爲當你發佈時,我已經是中途了。無論如何:
@Marco:
您可以創建一個26個計數器的臨時數組(初始化爲0)。
每個計數器對應於每個字母(即每個字母出現的數字)。例如counter[0]
對應於字母的出現次數的數量「A」,對於counter[1]
字母「b」,等...
然後經在輸入字符序列的每個字符和爲每個字符迭代做:
一個)獲取counter
數組中字符的索引。
b)增加counter["obtained index"]
由1
要獲得字符的索引你可以做到以下幾點:
一)首先要確保的字符不是資本,即只有「A」到「Z」允許,而不是'A'到'Z'。如果不是,請將其轉換。
b)從字符中去除字母「a」。 'a' - 'a'給出0,'b' - 'a'給出1,'c' - 'a'給出2等...
我將用C語言演示,因爲它是你的運動對MIPS(我指的目標是學習MIPS彙編語言):
#include <stdio.h>
int main()
{
//Maximum length of string:
int stringMaxLength = 100;
//Create string in stack. Size of string is length+1 to
//allow the '\0' character to mark the end of the string.
char str[stringMaxLength + 1];
//Read a string of maximum stringMaxLength characters:
puts("Enter string:");
scanf("%*s", stringMaxLength, str);
fflush(stdin);
//Create array of counters in stack:
int counter[26];
//Initialize the counters to 0:
int i;
for (i=0; i<26; ++i)
counter[i] = 0;
//Main counting loop:
for (i=0; str[i] != '\0'; ++i)
{
char tmp = str[i]; //Storing of str[i] in tmp, to write tmp if needed,
//instead of writing str[i] itself. Optional operation in this particular case.
if (tmp >= 'A' && tmp <= 'Z') //If the current character is upper:
tmp = tmp + 32; //Convert the character to lower.
if (tmp >= 'a' && tmp <='z') //If the character is a lower letter:
{
//Obtain the index of the letter in the array:
int index = tmp - 'a';
//Increment its counter by 1:
counter[index] = counter[index] + 1;
}
//Else if the chacacter is not a lower letter by now, we ignore it,
//or we could inform the user, for example, or we could ignore the
//whole string itself as invalid..
}
//Now find the maximum occurences of a letter:
int indexOfMaxCount = 0;
int maxCount = counter[0];
for (i=1; i<26; ++i)
if (counter[i] > maxCount)
{
maxCount = counter[i];
indexOfMaxCount = i;
}
//Convert the indexOfMaxCount back to the character it corresponds to:
char maxChar = 'a' + indexOfMaxCount;
//Inform the user of the letter with maximum occurences:
printf("Maximum %d occurences for letter '%c'.\n", maxCount, maxChar);
return 0;
}
如果你不明白,爲什麼我轉換上字母加32降低,然後閱讀:
每字符對應於內存中的一個整數值,並且當您對字符進行算術運算時,就像您在編碼表中將它們設置爲相應的編號一樣。
一個編碼只是一個表,它匹配那些字母和數字。
例如,'a'對應於ASCII encoding/decoding/table中的編號97。
例如'b'對應於ASCII encoding/decoding/table中的數字98。
所以'a'+ 1給出97 + 1 = 98這是字符'b'。它們都是內存中的數字,不同之處在於它們代表的是(解碼)它們。當然,編碼的同一張表也用於解碼。
例子:
printf("%c", 'a'); //Prints 'a'.
printf("%d", (int) 'a'); //Prints '97'.
printf("%c", (char) 97); //Prints 'a'.
printf("%d", 97); //Prints '97'.
printf("%d", (int) 'b'); //Prints '98'.
printf("%c", (char) (97 + 1)); //Prints 'b'.
printf("%c", (char) (((int) 'a') + 1)); //Prints 'b'.
//Etc...
//All the casting in the above examples is just for demonstration,
//it would work without them also, in this case.
非常感謝,非常有用的答案。明天我嘗試編碼,因爲我有一些疑問... 練習不區分大小寫,所以更容易編碼。 綜述: - 我創建了26整數(計數器,每一個字母) 數組 - 迭代字符串 的每一個字母 - 數組中更新相對指標 明天我嘗試並通知您新的問題...非常感謝你 – Marco
@Marco OK :)。忘記明確寫入以找到主循環後的最大出現次數,然後將索引轉換回字符,但我將它包含在代碼示例中,我認爲它應該是顯而易見的。我每天登錄(以獲取徽章:P),所以我會跟蹤你的意見。另外,歡迎來到StackOverflow :) – thanopi57
謝謝你的歡迎:)我跟着網站很長一段時間,但我沒有找到我的解決方案沒有註冊XD 我開始編碼,祝你好運我的自我hahaha 對不起,我的壞英語... – Marco