This assignment involves writing a C program to identify and

This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a sequence of characters that are not whitespace characters. Words are delimited by one or more whitespace characters. A whitespace character, such as a space, tab, or newline, is any character for which the function isspace() returns a value of true.

The operation of your program is controlled by the command line arguments that are passed to your program. Command line arguments consist of an optional set of flags arguments (a flag argument is an argument whose first character is a dash), followed by an optional list of filenames. If no filenames are specified, the program should read from standard input.

For each file specified (or, in the case of no file names specified, the standard input), the program must read the entire input and produce a count of characters, lines, and words. The output should be printed, one line per file, as follows:

Lines words characters filename

The numerical output (lines, words, characters) should be printed right justified in columns that are 12 characters wide. The filename should be printed left justified in the fourth column. In the case where the program in reading standard input, no filename should be printed.

If the program reads multiple files, then it should also provide a line at the end of the output with a total of all of the lines, words and characters in all of the files that were processed. The totals are tagged with the label “total”:

totalLines totalWords totalCharacters total

If the program is run with a flag in the form “-findchar=x”, then the program must also count the number of times it sees the character x in each file.

For example if I run the program by saying

program -findchar=m infile1 infile2

Then the output might be:

10 200 1000 infile1

20 150 1010 infile2

30 350 2010 total

m: 3 20 100 infile1

m: 0 0 0 infile2

m: 3 20 100 total

This means that the letter m appears on 3 different lines in infile1, in 20 different words, for a total of 100 times

If the program is run with a flag in the form “-findword=thisword”, then the program must count the number of times it sees the word thisword in each file.

Solution

#include<iostream>

#include<string>

#include<fstream>

#include<cstring>

using namespace std;

#include<ctype.h>

class Count{

string *files;

int *lc;

int *wc;

int *cc;

int size;

public:

Count(){ }

Count(string *f,int n){

   size=n;

   files = new string[n];

   for(int i=0;i<n;i++)

       files[i].assign(f[i]);

   lc= new int[n];

   wc = new int[n];

   cc=new int[n];

}

void countAll(){

for(int i=0;i<size;i++){

   ifstream ifs(files[i].c_str());

   if(!ifs.is_open()){

       cout<<\"Error in opening the file:\"<<files[i]<<endl;

       return;

   }

   int l=0,w=0,c=0;

   char ch;

   while(ifs.get(ch)){

       if(isalnum(ch))

           c++;

       if(ch==\' \')

           w++;

       if(ch==\'\ \'){

           l++;w++;

       }

   }

   ifs.close();

   lc[i]=l;

   wc[i]=w;

   cc[i]=c;

}

}

void printCount(){

   for(int i =0;i<size;i++)

       cout<<lc[i]<<\" \"<<wc[i]<<\" \"<<cc[i]<<\" \"<<files[i]<<endl;

}

void findChar(char m){

int totalLines=0,totalWords=0,totalChars=0;

   for(int i=0;i<size;i++){

       ifstream ifs(files[i].c_str());

       if(!ifs.is_open()){

           cout<<\"Error in opening the file\"<<endl;

           return ;

       }

       char ch;

       char lineflag=false,wordflag=false;

       while(ifs.get(ch)){

           if(ch==\'m\'){

               lineflag=true;

               wordflag=true;

               totalChars++;

           }

           if(ch==\' \' &&wordflag){

               wordflag=false;

               totalWords++;

           }

           if(ch==\'\ \' && lineflag) {

               totalLines++;

               lineflag=false;

           }

       }

       ifs.close();

   }

   printCount();

   cout<<totalLines<<\" \"<<totalWords<<\" \"<<totalChars<<\" \"<<\"total\"<<endl;

}

void findWord(string &word){

for(int i=0;i<size;i++)

{

   ifstream ifs(files[i].c_str());

   if(!ifs.is_open()){

       cout<<\"Error in opening the file\"<<endl;

       return ;

   }

   string tt;

   int wordCount=0;

   int lineCount=0;

   bool lineFlag=false;

   string ss;

   char ch;

   while(ifs.get(ch)){

       if(ch==\' \'){

           if(word.compare(ss)==0){

               wordCount++;

               lineFlag=true;

               ss.clear();

           }

       }

       if(isalnum(ch)) ss.append(1,ch);

       if(ch==\'\ \' && lineFlag){

           lineFlag=false;

           lineCount++;

       }

   }

   ifs.close();

   cout<<word<<\" \"<<wordCount<<\" \"<<lineCount<<\" \"<<files[i]<<endl;

}

}

};

void Usage(){

   cout<<\"count [-findchar=] [-findword=] [file1] [file2]....\"<<endl;

}

int main(int argc,char *argv[]){

if(argc==1){

   Usage();

   return 0;

}

bool findchFlag=false,findwFlag=false;

char findch;

char findw[20];

string name[20];

int fileCount=0;

char ch;

string word;

for(int i=1;i<argc;i++)

{

   if(strstr(argv[i],\"-findchar\")!=0)

   {

       cout<<argv[i]<<endl;

       int len=strlen(argv[i]);

     

       ch=argv[i][len-1];

       findchFlag=true;

   }

   else if(strstr(argv[i],\"-findword\")!=0)

   {

       cout<<argv[i]<<endl;

       char *tt=strtok(argv[i],\"=\");

       tt=strtok(NULL,\"=\");

       word.assign(tt);

       findwFlag=true;

   }

   else

       name[fileCount++].assign(argv[i]);

}

Count myCount(name,fileCount);

myCount.countAll();

myCount.printCount();

if(findchFlag)

   myCount.findChar(ch);

if(findwFlag)

   myCount.findWord(word);

return 0;

}

This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a s
This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a s
This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a s
This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a s
This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a s
This assignment involves writing a C++ program to identify and count lines, words, and characters. For the purpose of this assignment, we define a “word” as a s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site