Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment we are going to gather statistics about words in files. Our

ID: 3585229 • Letter: F

Question

For this assignment we are going to gather statistics about words in files. Our goal is to write a program in C++ that reads the text from files and determines information about the longest word and longest line in each file.

For the purpose of this assignment, a “line” is defined to be all characters up to a newline; in other words, what is fetched by the getline() function. A “word” is defined to be a sequence of one or more non-whitespace characters. Words are separated by whitespace (space, tab, newline, etc)

The default processing for each file is to read through the entire file, find the longest word and the longest line in the file, and generate output.

The format of the output for each file consists of a maximum of three lines

A line with the filename followed by a colon

A line with a sorted list of the words that are the longest words in the file, comma separated

A line containing a number which is the length of the longest line in the file

Use map to store the count of longest words and longest lines
Do the following program in c++ using command line arguments.
The accessing file should be using stream.

Explanation / Answer

As,I am going to use an ONLINE IDE to develop and verify my code ,so I am not using FILE for taking input, however I am reading input from console and generating the required output. AS in question you need to read different files and generate the output ,so here also you will be allowed to read diffrent input from console and output will be generated accordingly.

#include <iostream>

#include <string.h>

using namespace std;

#define MAX_WORDS 1000 //maximum number of words in a file

int main()

{

// your code goes here

int f,l,i,j;

cout<<"Enter number of files ";

cin>>f;

int cnt[MAX_WORDS];

while(f--)

{

cout<<"Enter number of lines in a particular file ";

cin>>l;

int arr[l];

memset(arr,0,sizeof(arr));

memset(cnt,0,sizeof(cnt));

int maxl=cnt[0];

int max=-1;

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

{

string str;

getline(cin,str); //this function is used to read line till it gets ' ' or new line

// fgets(str.c_str(),1000,stdin);

arr[i]=strlen(str.c_str());

if(arr[i]>max)

max=arr[i];

  

int len=0,k=0,s=0;

while(1)

{

if(str[k]==' ' || str[k]=='')

{

//check NULL

if(str[k]=='')

{

if(len>0)

{

cnt[s++]=len;

len=0;

}

break; //terminate the loop

}

cnt[s++]=len;

len=0;

}

else

{

len++;

}

k++;

}

for(k=0;k<s;k++)

{

if(cnt[k]>maxl)

maxl=cnt[k];

}

}

printf("maxmimum length of line in file:%d ",max);

printf("maxmimum length of word in file:%d ",maxl);

}

return 0;

}

YOU can use file handling concept to make your code and treat above code as your pseudo code.

If finding any difficulty in understanding the main logic comment !!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote