Please write a program to identify the most frequent letters that appear in the text for a given period.
Input formats:
Enter a given length of string in a row does not exceed 1000. String of ASCII code table any visible characters and spaces composition containing at least one English letter, end with a carriage return (Enter not count).
Output formats:
The maximum output frequency of the letters appear and the number of occurrences in a row, separated by a space between them. If there side by side, the output alphabetically minimum that letter. Case insensitive statistics, the output of lower-case letters.
Sample input:
This is a simple TEST. There ARE numbers and other symbols 1&2&3...........
Sample output:
e 7
-
Ideas: Hash
details: Direct find the maximum letter k -
code:
#include <bits/stdc++.h>
using namespace std;
int has[256];
int main(){
string s;
getline(cin, s);
for(int i = 0; i < s.size(); ++i){
if(islower(s[i])) has[s[i]]++;
if(isupper(s[i])) has[tolower(s[i])]++;
}
int k = 'a';
for(int i = 'a'; i <= 'z'; ++i){
if(has[i] > has[k]){
k = i;
}
}
printf("%c %d", k, has[k]);
return 0;
}