1. Read the contents of a data file (mytext.dat); 2. Process the frequency of ch
ID: 3594605 • Letter: 1
Question
1. Read the contents of a data file (mytext.dat); 2. Process the frequency of characters (a-z) that appear in the data file 3. Print the frequency of characters in the file to the screen; 4. Be able to use an ifstream object; In this assignment you will implement a program called "charword_freq.cpp" to determine the number of words and the number of occurrences of each letter in a block of text stored in a data file called “mytext.dat”. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma, or the beginning or end of a line. You can assume that the input consists entirely of letters, whitespace, commas and periods. When outputting the number of words and letters that occur in a line, be sure to count upper- and lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that do occur in the input data file. Consider the following example: Block of text in the data file: hello world ALL is great. HELLO WORLD ALL IS GREAT. hellO worlD alL iS great. Output of your program to the screen: 15 words 6 a 3 d 6 e 3 g 3 h 3 i 15 l 6 o 6 r 3 s 3 t 3 w Your program should be modular. broh
Explanation / Answer
Note: have to do some modifications to get correct output..I am doing it..
inputDataFile.txt
hello world ALL is great. HELLO WORLD ALL IS GREAT. hellO worlD alL iS great.
_______________
#include <fstream>
#include <iostream>
#include <cctype>
using namespace std;
int main() {
//defines an input stream for the data file
ifstream dataIn;
string word;
int count=0;
int alphabets[26]={0};
//Opening the input file
dataIn.open("inputDataFile.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
while(dataIn>>word)
{
count++;
for (int i = 0; i < word.length(); i++) {
if (((int) (tolower(word.at(i)) ))>= 97 && ((int) (tolower(word.at(i)))) <= 122)
{
alphabets[(int) (word.at(i)) - 97]++;
}
}
}
cout<<count<<" words"<<endl;
// Displaying the upper and lower character frequencies
for (int i = 0; i < 26; i++) {
if(alphabets[i]!=0)
{
cout<<char(97 + i)<<" "<<alphabets[i]<<endl;
}
}
//Closing the intput file
dataIn.close();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.