This is for C++, i will rate right away! Thanks! 17) Letter frequency analysis.
ID: 3930907 • Letter: T
Question
This is for C++, i will rate right away! Thanks!
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.
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
Explanation / Answer
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<<" "<<(float(it->second)/float(str.size()))<<endl;
}
}
int main () {
stringanalysis("AAsfdsadfasdfsdfdsaf");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.