Question

In: Computer Science

Please in C++ language Write a program that reads 10,000 words into an array of strings....

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.

Solutions

Expert Solution

#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.


Related Solutions

Programming lang C++ Write a program that reads 10,000 words into an array of strings. The...
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.
C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words)...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Use C language Write a program that reads in a series of lines of input character...
Use C language Write a program that reads in a series of lines of input character by character (using getchar()). The first line of the input contains an integer which specifies the number of remaining lines of input, each of which contains a floating point number. The integer value on the first line can be read with scanf(), but all of the following lines can only be read with getchar(). Each line after the first contains a single floating point...
Using C language: Write a program that asks the user for the size of an array,...
Using C language: Write a program that asks the user for the size of an array, then reads a number of integer values (from user input) into the array. Write a function to print out the array and call it to print the array out after all values are read in. Write a function to implement Insertion Sort and run it on the data array to sort the array. Write another function to implement Selection Sort and run it on...
(Please write in C++) Write a program that reads in a line consisting of a student’s...
(Please write in C++) Write a program that reads in a line consisting of a student’s name, Social Security number, user ID, and password. The program outputs the string in which all the digits of the Social Security number and all the characters in the password are replaced by x. (The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.) Your program should not use the operator [ ]...
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings...
Please write in x86 Assembly language on Visual Studio Write a program to compare two strings in locations str1 and str2. Initialize str1 to Computer and initialize str2 to Compater. Assume Str1 holds the correct spelling and str2 may have an incorrect spelling. Use string instructions to check if str2 is correct and if not correct the mistake in str2.
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT