1042 字符統計(20)(20 分)
請編寫程序,找出一段給定文字中出現最頻繁的那個英文字母。
輸入格式:
輸入在一行中給出一個長度不超過1000的字符串。字符串由ASCII碼錶中任意可見字符及空格組成,至少包含1個英文字母,以回車結束(回車不算在內)。
輸出格式:
在一行中輸出出現頻率最高的那個英文字母及其出現次數,其間以空格分隔。如果有並列,則輸出按字母序最小的那個字母。統計時不區分大小寫,輸出小寫字母。
輸入樣例:
This is a simple TEST. There ARE numbers and other symbols 1&2&3...........
輸出樣例:
#include "iostream"
#include "string"
using namespace std;
int main()
{
string s;
int a[26] = { 0 }, n = 0;
getline(cin,s); //遇到空格不結束
for (int i = 0; i < s.length(); i++)
s[i] = tolower(s[i]);//把大寫字母變小寫
for (int j = 0; j < s.length(); j++)
if (islower(s[j]))a[s[j] - 'a']++;//發現小寫字母,數組加1
int max = a[0];
for (int k = 1; k < 26; k++)//找出出現次數最多的
{
if (a[k] > max)
{
max = a[k];
n = k;
}
}
printf("%c %d", n + 'a', max);
}
e 7