In: Computer Science
Please in C++ language
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.
#include <string>
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <cctype>
using namespace std;
int countwords()
{
ifstream fin;
fin.open("file2.txt");
char word[30];
int count = 0;
while (!fin.eof())
{
fin >> word;
count++;
}
fin.close();
return count;
}
int main()
{
int no = 1000;//thousand for a thousand word
file
int num = countwords();//counts words in file 2 and
saves in num;
//******************************************************This
section finds and saves total words of file 1 in string
s2*************************
ifstream file1;
file1.open("file1.txt"); //defining the input file
stream for the first file
string s2[1000]; //array of string of 10000
elements
string lin;
int cnt = 0;
while (getline(file1, lin)) //reading the first file
and building the array
{
if (lin == "-1")
break;
istringstream str(lin);
string t;
while (getline(str, t, ' '))
{
//
cout << t << endl; this can be used to check if the
code is reading properly or not
if (t[0] <=
90)
t[0] = tolower(t[0]); //make it a small letter
if it is capital
if
(!isalpha(t[t.size() - 1]))
{
t.pop_back(); //if the character is . or ,
etc.
}
s2[cnt++] =
t;
if (cnt ==
no)
break;
}
if (cnt == no)
break;
}
file1.close();
//******************************************************This
section finds and saves total words of file 2 in string
s*************************
file1.open("file2.txt"); //defining the input file
stream for the first file
string s[num]; //array of string of 10000
elements
string line;
cnt = 0;
while (getline(file1, line)) //reading the first file
and building the array
{
if (line == "-1")
break;
istringstream str(line);
string t;
while (getline(str, t, ' '))
{
// cout << t
<< endl; this can be used to check if the code is reading
properly or not
if (t[0] <=
90)
t[0] = tolower(t[0]); //make it a small letter
if it is capital
if
(!isalpha(t[t.size() - 1]))
{
t.pop_back(); //if the character is . or ,
etc.
}
s[cnt++] =
t;
if (cnt ==
num)
break;
}
if (cnt == num)
break;
}
file1.close();
// s contains words from file 2 while s2 contains
words from file 1
//******************************************************This
section compares both strings and increments count if s is found in
s2*************************
int count = 0;
for (int i = 0; i < num; i++)
{
bool a = false;
for (int j = 0; j < no;
j++)
{
if (s[i] ==
s2[j])
{
a = true;
break;
}
}
if (a)
count++;
}
cout << " count of words of file 1 in file 2 are
: " << count<<endl;
system("pause");
return 0;
}
COMMENT DOWN BELOW FOR ANY QUERIES AND,
LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.