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
Thinking
- By
isalpha
determining whether the letter - With
transform(s.begin(),s.end(),s.begin(),::tolower);
the characters are converted to lowercase
Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin, s);
transform(s.begin(),s.end(),s.begin(),::tolower);
map<char,int> dict;
map<char,int>::iterator it;
for(int i=0;i<s.size();i++)
{
if(isalpha(s[i]))
{
it = dict.find(s[i]);
if(it != dict.end())
it->second += 1;
else
dict[s[i]] = 1;
}
}
char maxchar;
int maxvalue = -1;
for(it=dict.begin();it!=dict.end();it++)
{
if(it->second > maxvalue)
{
maxvalue = it->second;
maxchar = it->first;
}
}
cout << maxchar << " " << maxvalue;
return 0;
}
Quote
https://pintia.cn/problem-sets/994805260223102976/problems/994805280817135616