#include #include #include #include using namespace std; int main(int argc, char* argv[]) { char c; float total = 0.0; map freq_count; // initialize our 26 characters to zero for (int i = 0; i < 26; i++) { freq_count[(char)(i + 97)] = 0.0; } // open our file ifstream file(argv[1], ios::in); while ( ! file.eof() ) { // read in each character from the file file >> c; if ( (c >= 65) && (c <= 90) ) { // upper case letter freq_count[c + 32]++; total++; } else if ( (c >= 97) && (c <= 122) ) { // lowercase letter freq_count[c]++; total++; } } file.close(); // calculate the frequency of each character and print the result for (map::iterator im = freq_count.begin(); im != freq_count.end(); ++im) { cout << im->first << ": " << setiosflags(ios::fixed) << setprecision(2) << (im->second / total * 100.0) << std::endl; } }