+1 (812) 783-0640 

How to Write Assignment on Counting Words in a File Using Hash Table in C++

May 08, 2023
John Doe
John Doe
Canada
C++ programming
John Doe is a certified C++ programmer with 5+ years of experience in developing applications for various industries. He specializes in data structures and algorithms, object-oriented programming, GUI development, network programming, multi-threading, debugging, and performance tuning.

The task of counting the words in a given file is a frequent one for programming assignments. Hash tables in C++ can be used to complete this task. Data can be stored and retrieved quickly and effectively using hash tables. We'll walk you through the process of writing an assignment on word counting in a file using hash tables in C++ in this blog.

A typical programming task is to read the contents of a file, tokenize it into words, and then count the frequency of each word. This is known as word counting using a hash table. This blog post will discuss using hash tables to complete this task in C++.

Hash tables, also referred to as hash maps or dictionaries, are data structures that facilitate effective key-value pair storage and retrieval. The unordered_map class in C++ is used to implement hash tables and offers a quick and effective way to store and lookup key-value pairs.

We'll need to carry out the subsequent actions in C++ in order to count the words in a file using a hash table:

  1. Tokenize the file's content into words after reading it.
  2. The word counts should be kept in a hash table.
  3. By word count, sort the words.
  4. Write the findings to a file.

Now, let's look at each of these steps in more detail.

Understanding the Problem

It's critical to comprehend the issue prior to beginning the implementation. To read a file, count the number of times each word appears in the file, and output the results in descending order is the objective. The key steps are as follows:

  1. Read the document
  2. The file's content should be word-tokenized.
  3. Keep track of each word's count in a hash table.
  4. Sort the words in descending order of word count.
  5. Produce the words along with their counts in sorted order.

Reading the file and tokenizing its contents is the second step in using a hash table in C++ to count the number of words in it. Tokenizing is the process of dividing a file into tokens, or individual words. The std::stringstream class can be used to read a file line by line and then create another std::stringstream object to tokenize each line. This is one of the many ways to tokenize a file in C++.

To do this, we first create an object called a std::ifstream that symbolises the input file. Then, using the std::getline function, we create a std::stringstream object and read each line of the file into it. Next, each word or token is extracted from the line using another std::stringstream object that has been created. Then, just as in the previous step, we increment the hash table's count and add each token to it.

It's crucial to remember that handling punctuation and other non-word characters must be taken into account when tokenizing a file. To make sure we are only counting actual words, we may occasionally want to remove these characters from the tokens. Alternatively, if we're interested in examining the frequency of every character in the file, we might want to include them in the token count. The specific strategy will be determined by the demands of the current task.

Reading the File

Reading the file is the first step. The ifstream class in C++ can be used to read a file's contents. Here is an illustration of some code:

#include

#include

int main() {

    std::ifstream file("filename.txt");

    std::string word;

    while (file >> word) {

        // process each word

    }

    return 0;

}

This code opens the file "filename.txt" and reads each word from the file into the variable word until the end of the file is reached.

This code opens the file "filename.txt" and reads each word from the file into the variable word until the end of the file is reached.

Tokenizing the File

After reading the file, the next step is to turn its contents into words using tokenization. The stringstream class in C++ can be used to separate a string into words. Here is an illustration of some code:

#include

#include

int main() {

    std::string line = "hello world";

    std::stringstream ss(line);

    std::string word;

    while (ss >> word) {

        // process each word

    }

    return 0;

}

This code takes the string "hello world" and splits it into two words, "hello" and "world", which are stored in the variable word one at a time.

Storing Words and Counts in a Hash Table

The following step is to store each word and its count in a hash table after tokenizing the file into words. The unordered_map class in C++ can be used to create a hash table. Here is an illustration of some code:

#include

#include

int main() {

    std::unordered_map wordCounts;

    std::string word;

    while (/* read each word from file */) {

        if (wordCounts.find(word) == wordCounts.end()) {

            wordCounts[word] = 1;

        } else {

            wordCounts[word]++;

        }

    }

    return 0;

}

This code creates an empty unordered_map called wordCounts to store the counts for each word. Then, for each word in the file, it checks if the word is already in the hash table. If the word is not in the hash table, it adds the word with a count of 1. If the word is already in the hash table, it increments its count.

Sorting the Words by Count

The next step is to sort the words by their counts in descending order after you have stored the counts for each word in the hash table. The hash table can be sorted by value in C++ by using the sort algorithm. Here is an illustration of some code:

#include

#include

#include

int main() {

    std::unordered_map wordCounts;

    // read the file and store word counts in wordCounts

    std::vector> sortedWords(wordCounts.begin(), wordCounts.end());

    std::sort(sortedWords.begin(), sortedWords.end(), [](auto &left, auto &right) {

        return left.second > right.second;

    });

    return 0;

}

This code first creates a vector of pairs from the hash table, where each pair consists of a word and its count. Then, it sorts the vector of pairs by the second element (i.e., the count) in descending order using a lambda function.

Implementing the Code

It's time to put the code into action now that you have a better understanding of the issue and the steps necessary to solve it. Here is an illustration of its use:

#include

#include

#include

#include

#include

#include

int main() {

    std::unordered_map wordCounts;

    std::ifstream file("input.txt");

    std::string line;

    while (std::getline(file, line)) {

        std::stringstream ss(line);

        std::string word;

        while (ss >> word) {

            if (wordCounts.find(word) == wordCounts.end()) {

                wordCounts[word] = 1;

            } else {

                wordCounts[word]++;

            }

        }

    }

    std::vector> sortedWords(wordCounts.begin(), wordCounts.end());

    std::sort(sortedWords.begin(), sortedWords.end(), [](auto &left, auto &right) {

        return left.second > right.second;

    });

    std::ofstream output("output.txt");

    for (const auto &pair : sortedWords) {

        output << pair.first << " " << pair.second << std::endl;

    }

    return 0;

}

Sorting the Words by Count

The next step is to sort the words by their counts in descending order after you have stored the counts for each word in the hash table. The hash table can be sorted by value in C++ by using the sort algorithm. Here is an illustration of some code:

#include

#include

#include

int main() {

    std::unordered_map wordCounts;

    // read the file and store word counts in wordCounts

    std::vector> sortedWords(wordCounts.begin(), wordCounts.end());

    std::sort(sortedWords.begin(), sortedWords.end(), [](auto &left, auto &right) {

        return left.second > right.second;

    });

    return 0;

}

The hash table is first used to create a vector of pairs in which each pair contains a word and its count. The pairs vector is then sorted using a lambda function, starting with the second element (the count), and going down.

Implementing the Code

It's time to put the code into action now that you have a better understanding of the issue and the steps necessary to solve it. Here is an illustration of its use:

#include

#include

#include

#include

#include

#include

int main() {

    std::unordered_map wordCounts;

    std::ifstream file("input.txt");

    std::string line;

    while (std::getline(file, line)) {

        std::stringstream ss(line);

        std::string word;

        while (ss >> word) {

            if (wordCounts.find(word) == wordCounts.end()) {

                wordCounts[word] = 1;

            } else {

                wordCounts[word]++;

            }

        }

    }

    std::vector> sortedWords(wordCounts.begin(), wordCounts.end());

    std::sort(sortedWords.begin(), sortedWords.end(), [](auto &left, auto &right) {

        return left.second > right.second;

    });

    std::ofstream output("output.txt");

    for (const auto &pair : sortedWords) {

        output << pair.first << " " << pair.second << std::endl;

    }

    return 0;

}

This code reads the input file line by line, tokenizes each line into words, stores the word counts in a hash table, sorts the hash table by count in descending order, and writes the output to a file.

Conclusion

In conclusion, counting words in a file using hash tables in C++ can be done in a number of ways, including reading the file, tokenizing the content, storing the counts in a hash table, sorting the hash table by count, and printing the results. You ought to be able to write an assignment on this subject and test your implementation using the example code offered in this blog.

The process of using a hash table to count the frequency of each word in a file in C++ can be broken down into three main steps: reading the file, sorting the words according to their frequency using std::sort, and writing the results to a file using std::ofstream. You should be able to write a programme to count the words in any text file quickly and easily by following the instructions provided in this article.