I am writing a C++ program to count the frequency of a word occurring in a text file. I am using the isalpha function to separate the words, but isalpha does not differentiate between same strings having different punctuation.
For example: “I own a company. In my company, there are 200 employees. I love my company.”
In the above sentence it gives count of company as 3
How do I make it to differentiate the count like:
company 1
company, 1
company. 1
The loop which does the counting:
while(!isalpha(c) && !in.eof())
{
c = in.get();
}
while(isalpha(c))
{
out.push_back(tolower(c));
c = in.get();
}
where ‘out’ is a string, ‘in’ is an istream value and c is a char.
[EDIT] Got Solution
while(!isalpha(c) && !ispunct(c) !in.eof())
{
c = in.get();
}
while(isalpha(c) || ispunct(c))
{
out.push_back(tolower(c));
c = in.get();
}
I thought your way was better (I would expect company to have a count of 3).
But if you want to separate words and punctuation is significant, then use isspace() as a separator.
Note: The standard stream >> operator already does this for you.