This is for c i will rate right away Hint Make use of arrays
This is for c++, i will rate right away!
Hint: Make use of arrays to make this much easier
Hint: Don’t worry about capitalization – convert everything to upper or lower case.
Hint: Ignore characters that are not letters
Hint: To get the frequency, you also need to know how many letters are in the string
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. Example output file: Encrypted text is sometimes achieved by replacing one letter by another. To start deciphering the encryption 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 Q: 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
Solution
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void freq(string s)
{
int len = s.length();
int a[26];
for(int i=0;i<26;i++)
{
a[i]=0;
}
int count=0;
for(int i=0;i<len;i++)
{
char ch = s.at(i);
if(ch>=\'a\' && ch<=\'z\')
{
a[ch-\'a\']++;
count++;
}
else if(ch>=\'A\' && ch<=\'Z\')
{
a[ch-\'A\']++;
count++;
}
}
ofstream out;
out.open(\"freq_out.txt\");
out << setprecision(2) <<fixed;
for(int i=0;i<26;i++)
{
if(i>0)
{
out<<\' \';
}
char ch = \'A\'+i;
out<<ch<<\' \'<<(a[i]*1.0)/count;
}
out<<endl;
out.close();
}
int main()
{
freq(\"rajeev\");
return 0;
}

