Skip to content

Instantly share code, notes, and snippets.

@mehmetakifakkus
Last active November 23, 2016 13:08
Show Gist options
  • Save mehmetakifakkus/b057727679138c4b4612d97bcd9db835 to your computer and use it in GitHub Desktop.
Save mehmetakifakkus/b057727679138c4b4612d97bcd9db835 to your computer and use it in GitHub Desktop.
Character frequency in a text
// Example program
#include <iostream>
#include <string>
#include <map>
#include <cstddef>
int main()
{
std::string text = "Mr Trump, who has flown to Florida for the Thanksgiving holiday on Thursday, is still assembling his White House team. One of America's top generals, David Petraeus, has told the BBC he would be willing to serve under him.";
std::map<char, std::size_t> freq;
for(auto c: text)
{
if( freq.find(c) == freq.end())
freq[c] = 1;
else
++freq[c];
}
int i=0;
for(auto elem: freq)
{
std::cout << i++ <<". "<< elem.first << " -> " << elem.second << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment