C++ program to find a given word in the
files file1.txt and file2.txt and display the number of occurrences of that
word. Pass the arguments through command line.
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char *argv[])
{
int occ=0;
char word[20];
int n=500;
char *a=new char[n];
int k=0,i=0,j=0;
cout<<"enter the word to be searched\n";
cin.getline(word,10);
int w_size=0;
for(int i=0;word[i]!='\0';i++)
{
w_size++;
}
ifstream fin[2];
for(int m=1,o=0;m<=2,o<=1;m++,o++)
{
fin[o].open(argv[m],ios::in);
if(fin[o].is_open())
{
cout<<"the
file("<<argv[m]<<") opened successfully!!!\n";
while(!fin[o].eof() && k<n)
{
fin[o].get(a[k]);
k++;
}
a[k-1]='\0';
for(i=0;a[i]!='\0';i++)
{
for(j=0;word[j]!='\0';j++)
{
if(a[i]!=word[j])
{
break;
}
else
{
i++;
if(word[j+1]=='\0')
{
occ++;
}//if
}//else
}//for
}//for
if(occ==0)
{
cout<<"word do not exist!!\n";
}
}//if
else
{
cout<<"the
file("<<argv[m]<<") could not be opened!!\n";
}
fin[o].close();
}
cout<<"total occurences found :
"<<occ<<endl;
return 0;
}
No comments:
Post a Comment