C++ Create a program:  Given a large text file with multiple strings, what would be the most efficient way to read the text file and count how many and which word appears the most words in c++. The text file's size is unknown.  I add an image for input .

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

C++
Create a program: 


Given a large text file with multiple strings, what would be the most efficient way to read the text file and count how many and which word appears the most words in c++.
The text file's size is unknown. 

I add an image for input . 

 

"C:\Users\Klemen\Desktop\Pomembni Programi\VajeCPP\datoteke\bin\Debug\datoteke.exe"
>TABLE OF MOST COMMON WORDS<
*100
*58
*49
*42
*40
*35
*32
*25
*22
*21
*21
*20
*20
*17
*16
*14
*14
*14
*14
*13
|11.72%
6.8%
5.74%
4.92%
4.69%
4.1%
3.75%
2.93%
2.58%
2.46%
2.46%
2.34%
2.34%
1.99%
1.88%
1.64%
1.64%
1.64%
1.64%
|1.52%
Sy
the
is
3
to
of
ke
rce
7.
ma
you
a
8
and
it
law
in
that
be
there
for
will
if
9
10.
rce
11
12.
ma
13.
14.
15.
16.
17.
18.
19.
20.
|сan
are
not
->Dif. strings:
853<------
Process returned 0 (0x0>
Press any key to continue.
execution time
: 2.023 s
Code::Blocks
XQ Search results
XA Ccc
Build log
Build messages
Transcribed Image Text:"C:\Users\Klemen\Desktop\Pomembni Programi\VajeCPP\datoteke\bin\Debug\datoteke.exe" >TABLE OF MOST COMMON WORDS< *100 *58 *49 *42 *40 *35 *32 *25 *22 *21 *21 *20 *20 *17 *16 *14 *14 *14 *14 *13 |11.72% 6.8% 5.74% 4.92% 4.69% 4.1% 3.75% 2.93% 2.58% 2.46% 2.46% 2.34% 2.34% 1.99% 1.88% 1.64% 1.64% 1.64% 1.64% |1.52% Sy the is 3 to of ke rce 7. ma you a 8 and it law in that be there for will if 9 10. rce 11 12. ma 13. 14. 15. 16. 17. 18. 19. 20. |сan are not ->Dif. strings: 853<------ Process returned 0 (0x0> Press any key to continue. execution time : 2.023 s Code::Blocks XQ Search results XA Ccc Build log Build messages
Expert Solution
Step 1

NOTE:- The output of the following program may not be as per the given image but the main problem is to sort the words on the basis of their frequency in descending order that has been completed.

you can add the columns of percentage and serial number that is easy part.

 

Program Plan:-

  1. Initialize all the required header files.
  2. Read the file of which the words are to be counted. 
  3. take the map of string and integer. 
  4. count the frequency of each word.
  5. sort the map on the basis of the value in descending order.
  6. finally call the print map function to print the values of the map. 
Step 2

The following is the required C++ program-

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

//comparing map on the basis of second element
bool compare( const pair <string, int> &a, const pair<string, int> &b)
{
    return (a.second < b.second);
}
//the function for printing the map
void PrintMap(vector<pair<string, int>> m)
{
    vector<pair<string, int>>::iterator itr;
    for ( itr = m.end()-1; itr >= m.begin(); itr--)
        cout << itr->first << ": " << itr->second << endl;
}

int main(void)
{
    string fileName;
    cout<<"Enter the file name\n";
    cin>>fileName;

    // for storing the words and counting them
    map<string, int> cntWord;

    {
        //Read the file
        ifstream file(fileName);

        //checking if the file is opened
        if (file.is_open())
            while (file.good())
            {
                //storing the word in the local variable
                string word;
                file >> word;

                //look if it is repeated.
                if (cntWord.find(word) == cntWord.end())
                    cntWord[word] = 1;
                else //the word is seen before
                    cntWord[word]++; //increment its count.
            }
        else
        {
            cerr << "Couldn't open the file." << endl;
            return EXIT_FAILURE;
        }

        vector<pair<string, int>> vec;

        // copy key-value pairs from the map to the vector
        map<string, int> :: iterator it2;
        for (it2=cntWord.begin(); it2!=cntWord.end(); it2++)
        {
            vec.push_back(make_pair(it2->first, it2->second));
        }
        //sorting the map
        sort(vec.begin(),vec.end(),compare);

        // Print the words map.
        PrintMap(vec);
    }

    return EXIT_SUCCESS;
}

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education