Question

In: Computer Science

In c++ please. Write a program that reads the contents of two text files and compares...

In c++ please.

Write a program that reads the contents of two text files and compares them in the following ways:

It should display a list of all the unique words contained in both files.

It should display a list of the words that appears in both files.

It should display a list of the words that appears in the first file, but not the second.

It should display a list of the words that appears in the second file, but not the first.

It should display a list of the words that appears in either the first or second file, but not both.

Solutions

Expert Solution

C++ Code:

#include <bits/stdc++.h>
using namespace std;

//map for two files.
map<string,int>set1;
map<string,int>set2;

// driver code
int main()
{
   // filestream variable file
   fstream file1,file2;
   string word, t, q, filename,filename2;


   // filename of the file
   filename = "paragraph.txt";
   filename2= "paragraph2.txt";

   // opening file
   file1.open(filename.c_str());
   file2.open(filename2.c_str());

   // extracting words from the file
   while (file1 >> word)

       set1.insert({word,1});

   }

cout<<"Unique Word of text file 1:\n\n";
   while (file2 >> word)
   {

        set2.insert({word,1});
   }

   //print unique word of file1.

   for(auto i=set1.begin();i!=set1.end();i++)
    {
      cout<<i->first<<" ";                 //print the words.
    }
cout<<"\n\n\nUnique Word of text file 2:\n\n";
   //print unique word of file2.
    for(auto i=set2.begin();i!=set2.end();i++)
    {
      cout<<i->first<<" ";                 //print the words.
    }

cout<<"\n\n\n Words that appears in both files:\n\n";
    for(auto i=set1.begin();i!=set1.end();i++)
    {

        if(set2[i->first]==1)
            cout<<i->first<<" ";
    }

cout<<"\n\n\n words that appears in the first file, but not the second:\n\n";
    for(auto i=set1.begin();i!=set1.end();i++)
    {

        if(set2[i->first]==0)
            cout<<i->first<<" ";
    }

cout<<"\n\n\n words that appears in the second file, but not the first:\n\n";
    for(auto i=set2.begin();i!=set2.end();i++)
    {

        if(set1[i->first]==0)
            cout<<i->first<<" ";
    }

cout<<"\n\n\n words that appears in either the first or second file, but not both:\n\n";
    for(auto i=set1.begin();i!=set1.end();i++)
    {

        if(set2[i->first]==0)
            cout<<i->first<<" ";
    }
    for(auto i=set2.begin();i!=set2.end();i++)
    {

        if(set1[i->first]==0)
            cout<<i->first<<" ";
    }
   return 0;
}

Output:


Related Solutions

File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT