How to find the number of occurrences of a word in a text file


I am doing a program on files.The program should read a file like example.txt and find the number of occurrences of a word.for example say
Example.txt contains :

apple apple sugar meat milk groundnut milk

output :

name number of occurrences
apple 2
sugar 1
meat 2
...


I have done the program like this :


#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
ifstream myfile2("example.txt");
vector<string> data;
vector<string>::const_iterator iter;

char text[10];
if(myfile2.is_open())
{
while(!myfile2.eof())
{
myfile2>>text;
data.push_back(text);
}
}
cout<<"The data is :";
for(iter = data.begin();iter != data.end();iter++)
{
cout<<*iter<<endl;
}


myfile2.close();
system("pause");
return 0;
}



Hmm, not bad. Look, try to format your code and put it into code-tags (BTW).
You can do it like this:

1
2
3
4
std::ifstream filestream("file.txt");
std::ostringstream datastream << filestream.rdbuf() << '\n';
std::string strdata(datastream.str());
std::map<std::string, size_t> occurences;


No, to check the amount of occurences of a text, use a loop:
 
for(size_t found(-1);(found = strdata.find("apple", found + 1)) != std::string::npos;++occurences["apple"]);

you should better use map to store data.
map<string, int count> You should read each word from the file not the line
whenever you extract a word try to find in the map
if found then increment the count
else insert the entry with count as 1.


So at the end when you will traverse the map you can exact number of words occurances in file.
you should better use map to store data.
Thats what i said..(?)
map<string, int count> You should read each word from the file not the line


it should be std::size_t, not int. A word can occur -1 times.

whenever you extract a word try to find in the map
if found then increment the count
else insert the entry with count as 1.


I think you didn't really get the point with maps. They do exactly that for you, so you don't have to care about that.
Last edited on
Topic archived. No new replies allowed.