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

Write a function inputStats that takes an istream& and an ostream& as parameters

ID: 3812695 • Letter: W

Question

Write a function inputStats that takes an istream& and an ostream& as parameters. The input stream represents an input file. Your function reports various statistics about the file's text. In particular, your function should report the number of lines in the file, the longest line, the number of tokens on each line, and the length of the longest token on each line. You may assume that the input file has at least one line of input and that each line has at least one token. For example, if an input stream named input opened with the file carroll.txt which contains the following text:

"Beware the Jabberwock, my son,

the jaws that bite, the claws that catch,

Beware the JubJub bird and shun

the frumious bandersnatch."

The call to inputStats(input, cout); should produce the following output: Line 1

Line 1 has 5 tokens (longest = 11)

Line 2 has 8 tokens (longest = 6)

Line 3 has 6 tokens (longest = 6)

Line 4 has 3 tokens (longest = 14)

Longest line: the jaws that bite, the claws that catch,

its c++. not java.

Explanation / Answer

//here the code is divided into two parts 1st is the header file and next is the main code

//wordlen.h


#include <string>
#include <vector>
#include <iostream>

class InputStats
{
public:
static std::wstring lines;
static std::wstring bigestline;
static void main(std::vector<std::wstring> &args) throw(FileNotFoundException);
   static void inputStats(Scanner *input) throw(FileNotFoundException);
};

//main_code.cpp

#include "wordlen.h"

std::wstring InputStats::lines = L"";
std::wstring InputStats::bigestline = L"";

void InputStats::main(std::vector<std::wstring> &args) throw(FileNotFoundException)
{
   Scanner *console = new Scanner(System::in);
   std::wcout << L" " << std::endl;
   std::wcout << std::endl;
   std::wcout << L"Please enter a name of a file " << std::endl;
   std::wstring name = console->nextLine();
   File tempVar(name);
   Scanner *input = new Scanner(&tempVar);
   while (input->hasNextLine())
   {
   std::wstring line = input->nextLine();
   Scanner tempVar2(line);
   inputStats(&tempVar2);
   }

std::vector<std::wstring> line = lines.split(L":");
bigestline = line[0];
for (int j = 0;j < line.size() - 1;j++)
{
if (bigestline.length() < line[j + 1].length())
{
   bigestline = line[j + 1];
}
}

std::wcout << L" Biggest Line in File=" << bigestline << L" ";

}

void InputStats::inputStats(Scanner *input) throw(FileNotFoundException)
{
   int numLines = 0;
   int numwords = 0;
   std::wstring maxLine = L"";

   while (input->hasNextLine())
   {
       std::wstring next = input->nextLine();
       std::wcout << L" " << next;
       lines = lines + L":" + next;

       numwords += next.split(L"\s+")->length;
       numLines++;

   if (next.length() > maxLine.length())
   {
   maxLine = next;
   }
   }

   std::wcout << L" Line " << numLines << L" has ";
   std::wcout << numwords << L"tokens ";
   std::wcout << L"(longest = " << maxLine.length() << L")";
   std::wcout << std::endl;
}

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