I dont know how to implement those coding in this question , anyone can help?

In this question, you need to develop a word counting application. It will read any given text file and do the following:-

a)Count the total number of words in the text and list the top 10 most frequently used words in the text.

For example a file with the following text,

Mary had a little lamb
Little lamb, little lamb
Mary had a little lamb


Create a linked-list data structure to maintain the data

EMPTY-> word = lamb -> word = little -> word = a -> word = had -> word=mary
count = 4 count=4 count=2 count=2 count=2

Your program should produce the following output:-

Filename= input.txt
Total words = 16
Results (5)
1. (lamb,4)
2.(little,4)
3.(a,2)
4.(had,2)
5.(mary,2)

When i am counting the total words for the text , my progrm just recgonize 14 words instead of 16 words , and the program cannot recognize the same words inside the text even one of them is using uppercase letter like lamb , it is just count as 3 instead of 4. Anyone can help to solve this problem and provide the solution




yexuan wrote:
I dont know how to implement those coding in this question

Have you finished counting all words?
When i am counting the total words for the text , my progrm just recgonize 14 words instead of 16 words

What?? No matter how I look at it your program is right since there are exactly 14 words in the paragraph.
Mary had a little lamb
Little lamb, little lamb
Mary had a little lamb


???
Last edited on
Ya i already try to counting all the words and this is my coding :

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(void)
{
ifstream inFile;
string fileName;
char str[80];
int count = 0;

cout << "Please enter the file name: ";
cin.getline(str, 80);

inFile.open(fileName.c_str());

while (!inFile.eof())
{
for (int i = 0; str[i] != '\0'; i++)
if (str[i] == ' ') //Checking for spaces
{
count++;
}
}

cout << "Number of words in file is : " << count;


inFile.close();

system("pause");
return 0;
}

I dont kow what is the problem of this question lol , and i dont know how to list the top 10 most frequently used words in the text by using linked list data structure, can someone help me =.=?
Presumably you want 'little' and 'Little' to both count as 'little' and 'lamb' and 'lamb,' to both count as 'lamb'? In which case you need to do things first-off: (a) set the entire string to lower-case (or upper-case) and (b) remove all punctuations; if there are any additional checks that need to be done on the incoming data then put them in place as well.
The following program does the above two transformations and stores the data in a std::map<std::string, int> where the map key(std::string) is the word read from the file with punctuation and uppercases removed and the map value (int) is the number of time that word appears in the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <cctype>//::tolower, ::ispunct//
#include <algorithm>
#include <fstream>
#include <map>

std::string& removePunctAndToLower(std::string& original)
{
    original.erase (std::remove_if (original.begin (), original.end (), ::ispunct), original.end ());
    //removes punctuation: http://stackoverflow.com/questions/19138983/c-remove-punctuation-from-string
    std::transform(original.begin(), original.end(), original.begin(), ::tolower);
    //lowercases the string throughout: http://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case
    return original;
}
int main()
{
    std::ifstream inFile("F:\\test.txt");
    std::map<std::string, int> myMap{};
    if(inFile)
    {
        std::string temp{};
        while (inFile >> temp)
        {
            if(inFile)
            {
                removePunctAndToLower(temp);
                myMap[temp]++;
            }
        }
    }
    for (const auto& elem : myMap)
    {
        std::cout << elem.first << " " << elem.second << '\n';
    }
}

Now the next part of your problem is to make a linked list out of it: the Node (struct/class) of the list would have data-members std::string its_word, int its_count etc and you can pass each element of myMap to an appropriate overloaded Node ctor that would create the corresponding Node objects, separately there'd be a LinkedList(struct/class) having the appropriate Node pointers, etc.
You don't mention whether it should be a singly or doubly linked list, here's a reference to a single linked list that you could, if you wish, edit for your needs though there are many others (often far superior) on the net:
http://www.cplusplus.com/forum/beginner/208086/#msg981383
Thanks bro , i get it already :D You are awesome :)
Topic archived. No new replies allowed.