Letter frequency analysis Write a function or series of func
Letter frequency analysis. Write a function or series of function that when used will create a letter frequency analysis file. The function should take a string as a parameter. It should process this string one character at a time. The function(s) should create and output a file containing the string analyzed and letter frequency analysis of that string. Your frequencies should add up to 1.0 or 100% depending on implementation. The program is being written in C++. I have attached a picture of the question along with an example output file.
17) Letter frequency analysis. Write a function or series of function that when used will create a letter frequency analysis file. The function should take a string as a parameter. It should process this string one character at a time. The function (s) should create and output a file containing the string analyzed and letter frequency analysis of that string. Your frequencies should add up to 1.0 or 100% depending on implementation. (8) Example output file: Encrypted text is sometimes achieved by replacing one letter by another. To start deciphering the encrvption it is useful to get a frequency count of all the letters. The most frequent letter may represent the most common letter in English E followed by T A, O and I whereas the least frequent are Q, z and X A: 0.05 B: 0.01 C: 0.03 D: 0.02 E: 0.17 F: 0.02 G: 0.02 H: 0.04 I 0.05 J: 0.00 K: 0.00 L: 0.05 M: 0.03 N: 0.07 O: 0.06 P: 0.02 O: 0.02 R: 0.07 S: 0.05 T: 0.13 U: 0.02 V: 0.00 W 0.01 X: 0.01 Y: 0.03 Z: 0.00 Hint: Make use of arrays to make this much eas7er Hint: Don\'t worry about capitalization convert everything to upper or Tower case Hint: Ignore characters that are not letters Hint: To get the frequency, you also need to know how many Tetters are in the stringSolution
Code:
#include <cstdio> // std:s:cin, std::cout
#include<map>
#include <fstream>
#include <iostream>
using namespace std;
void stringanalysis(string str)
{
int i=0;
ofstream outfile;
outfile.open(\"ex.txt\");
outfile<<\"string analysis: \"<<endl;
map<char,int> hash;
while(i<str.size())
{
if(hash.count(str[i])==0)
hash[str[i]]=1;
else
hash[str[i]]++;
i++;
}
map<char,int>::iterator it =hash.begin();
for(it=hash.begin();it!=hash.end();it++)
{
outfile<<it->first<<\" \"<<it->second<<endl;
}
}
int main () {
stringanalysis(\"AAsfdsadfasdfsdfdsaf\");
return 0;
}
it generates file ex.txt:
string analysis:
A 2
a 3
d 5
f 5
s 5
