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

Respuesta :

Answer:

NOTE: Please note that the below program is tested on ubuntu 14.04 system and compiled under g++ compiler.

Program....

-------------------------------------------------------------------------------------------------------------------------------------

//Heade File Declaration

#include<iostream>

#include<stdlib.h>

#include<string.h>

#include<fstream>

using namespace std;

//A function prototype

void reading_file(ifstream& myfile);

int main()

{

ifstream myfile;

//Call to reading_file function

reading_file(myfile);

return 0;

}

//Function defination

void reading_file(ifstream& myfile)

{

string input;

//Read the text file

myfile.open("mytext.dat");

//reads the data of the file by opening it

if (myfile.is_open())

{

//reading lines of the file

while (getline(myfile, input))

{

int count[26] = { 0 }, i, wordCount = 0;

//checking cases

for (i = 0; i < input.size(); i++)

{

if (((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z')) && (input[i + 1]

== '.' || input[i + 1] == ',' || input[i + 1] == ' ')) wordCount++;

if (input[i] >= 'a' && input[i] <= 'z') count[input[i] - 'a']++;

if (input[i] >= 'A' && input[i] <= 'Z') count[input[i] - 'A']++;

}

//number of words

char x = input[input.size() - 1];

if (x != '.' && x != 'z' && x != ' ')

wordCount++;

cout << endl<< wordCount << " words" << endl;

for (i = 0; i < 26; i++)

{

if (count[i] > 0) cout << count[i] << " " << (char)('a' + i) << endl;

}

}

//closes file

myfile.close();

}

else cout << "file unable to open";

}

Explanation:

Please kindly see attachment for output.

Ver imagen Jerryojabo1
ACCESS MORE
EDU ACCESS