Write a function called countwords This function is passed t

Write a function called count_words. This function is passed the name of a file, and should count the number of times each word occurs in the file. A dictionary should be returned, whose keys are words, and whose values are their counts. For example:

>>> freq = count_words(\'hamlet.txt\')
>>> freq[\'to\']
13

Solution

I am going to share the code of the given question if you face any trouble in compiling the code or in understanding of the code feel free to ask in the comment section below.

Function will be called like this:

#include<iostream.h>

#include<fstream.h>

int count_words(

{

int w=0; // For counting the word which is passed in function

fin.seekg(0,ios::end); //point the file pointer at the end of the file

size=fin.tellg(); // For counting number of bytes till current position of the pointer fin.seekg(0,ios::beg); //Point at the beginning of the file

while(fin)

{

fin.get(c)

  if(c==\'n\'||c==\' \')

   w++;

  

}

cout<<\"nWords=\"<<w <<\"n\";

fin.close(); //File is closed

return 0;

Write a function called count_words. This function is passed the name of a file, and should count the number of times each word occurs in the file. A dictionary

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site