In: Computer Science
Programming lang C++
Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
//eng.txt -- file that contains 10,000 words here for testing purpose I have taken only 17/18 words
program
will
then
read
a
second
file
that
contains
an
undetermined
number
of
words
and
mister
search
them
------------------------------------
//example.txt ---file containing undetermined no of words
hello
program
and
or
display
--------------------------
//main.cpp
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
///function to check if word is present in string array having size
'size'
///return true if found else false
///sequential search needs O(n) time
bool searchWord(string strArray[],int size ,string word)
{
for(int i=0;i<size;i++){
if(word == strArray[i]) ///word found
{
//cout<<"*"<<word<<"*"<<word.size()<<endl;
return true;
}
}
return false;
}
int main()
{
char file1[]="eng.txt"; /// first file name containing 10000
words
char file2[]="example.txt"; /// first file name containing words to
be search
string strArray[10000];
string testArray[100]; /// consider MAX 100 words are present
///.. You can change this max value but check your system memory
availability
int cnt1=0; /// counter to measure no of words present in first
file
int cnt2=0; /// counter to measure no of words present in second
file
///reading first file
fstream out;
string line;
out.open(file1,ios::in); ///open in read mode
if(out.is_open()){ ///if file exits
while(!out.eof()){ /// read file line by line
getline(out,line);
if(line.size()>0)
strArray[cnt1++]=line; ///store the line in string array &
increment cnt1
}
}
else{
cout<<"\nThe file with name "<<file1<<" not
present ";
exit(1);
}
out.close(); ///close file
/*for(int i=0;i<cnt1;i++){
cout<<"\n"<<strArray[i];
}*/
///now read second file
out.open(file2 , ios::in); ///open in read mode
if(out.is_open()){ ///if file exits
while(!out.eof()){ /// read file line by line
getline(out,line);
if(line.size()>0)
testArray[cnt2++]=line; ///store the line in test array &
increment cnt1
}
}
else{
cout<<"\nThe file with name "<<file2<<" not
present ";
exit(1);
}
out.close(); ///close file
/*
cout<<"\nThe english file contains total
"<<cnt1<<" words\n";
cout<<"\nTest array \n\n";
for(int i=0;i<cnt2;i++){
cout<<"\n"<<testArray[i];
}
*/
if(cnt1<=0 || cnt2<=0){
cout<<"\nOne of the given file is empty";
exit(0);
}
cout<<"\nThe test file contains total "<<cnt2<<"
words\n";
///Now simply search each word in testArray wrt strArray
int total=0; ///total words that are present in both files
for(int i=0;i<cnt2;i++){
if( searchWord(strArray , cnt1 ,testArray[i]) &&
testArray[i]!="\n")
total++; ///if word found increment total
}
cout<<"\nThe test file contains "<<total<<"
words that are present in both files\n";
if(total<cnt2-1){
cout<<"\nThe test file contains
"<<(cnt2-total)<<" words that are not present in first
file";
}
return 0;
}
--------------------------------
if same filename is provided then ->
if file2 contains more than file1