Last active
November 23, 2016 13:08
-
-
Save mehmetakifakkus/b057727679138c4b4612d97bcd9db835 to your computer and use it in GitHub Desktop.
Character frequency in a text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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