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

Solution

Code:

#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;
int tc,tw,tl;

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];
   tc=tw=tl=0;
}

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;  

   tl+=l;
   tw+=w;
   tc+=c;

}

}

void printCount(){
   for(int i =0;i<size;i++)
       cout<<lc[i]<<\" \"<<wc[i]<<\" \"<<cc[i]<<\" \"<<files[i]<<endl;

   cout<<tl<<\" \"<<tw<<\" \"<<tc<<\" total\"<<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 ;
       }

       int lines=0,chars=0,words=0;

       char ch;
       char lineflag=false,wordflag=false;
       while(ifs.get(ch)){
           if(ch==\'m\'){
               lineflag=true;
               wordflag=true;
               totalChars++;
               chars++;
           }
           if(ch==\' \' &&wordflag){
               wordflag=false;
               totalWords++;
               words++;
           }

           if(ch==\'\ \' && lineflag) {
               totalLines++;  
               lineflag=false;
               lines++;
           }
       }
       ifs.close();

       cout<<m<<\":\"<<lines<<\" \"<<words<<\" \"<<chars<<\" \"<<files[i].c_str()<<endl;
   }

   cout<<m<<\":\"<<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)
   {
       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();

cout<<\"Files data information:\"<<endl;
myCount.printCount();

if(findchFlag){
   cout<<\"Finding char : \"<<ch<<\" in file(s)\"<<endl;
   myCount.findChar(ch);
}
if(findwFlag){
   cout<<\"Finding word : \"<<word<<\" in file(s)\"<<endl;
   myCount.findWord(word);
}

return 0;
}

Output:

Files data information:
3 10 47 tmp.txt
3 10 54 tmp2.txt
6 20 101 total
Finding char : m in file(s)
m:2 3 4 tmp.txt
m:3 4 6 tmp2.txt
m:5 7 10 total

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site