问题描述:
判断一个字符串中a~z哪个字母出现的次数最多;
样例:
输入:
2
abbccc
adfadffasdf
输出:
c 3
f 4
代码展示:
#include <iostream>
#include <string.h>
using namespace std;
void SumOfChar(char S[])
{
int sum[26] = {0};
unsigned i = 0, max = 0, temp = 0;
char ch;
while (i != strlen(S))
{
temp = ++sum[S[i++] - 'a'];
if (temp > max)
{
max = temp;
ch = S[i-1];
}
}
cout << ch << "\t" << max << endl;
}
int main()
{
char S[1001];
int num = 0;
cin >> num;
while (num--)
{
cin >> S;
SumOfChar(S);
}
return 0;
}