#include <iostream>
#include <iomanip>
#include <sstream>
#include <cctype>
std::istringstream in(
"Hi all,\n"
"\n"
"I would like to write a program to count the number of characters, "
"words, and paragraphs(or newlines) in a file.I have the characters "
"counting correctly and the newlines counting correctly but I am "
"stuck on how to count the words, I would like to store the words in "
"an array to hold the size of the word(i.e.the number of letters "
"influences where its stored in the array) then I need to sum up the "
"array and display the output.\n"
"\n"
"Thanks in advance for the help."
);
int main()
{
std::size_t characters = 0;
std::size_t words = 0;
std::size_t paragraphs = 0;
bool in_word = false;
bool in_paragraph = false;
char token;
while (in.get(token))
{
if (std::isspace(token))
{ // whitespace
in_word = false;
if (token == '\n')
in_paragraph = false;
}
else
{ // non-whitespace
++characters;
if (!in_paragraph)
{
in_paragraph = true;
++paragraphs;
}
if (!in_word)
{
in_word = true;
++words;
}
}
}
std::cout << "Characters:" << std::setw(5) << characters << '\n';
std::cout << "Words: " << std::setw(5) << words << '\n';
std::cout << "Paragraphs:" << std::setw(5) << paragraphs << '\n';
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPHNzdHJlYW0+CiNpbmNsdWRlIDxjY3R5cGU+CgpzdGQ6OmlzdHJpbmdzdHJlYW0gaW4oCiAgICAiSGkgYWxsLFxuIgogICAgIlxuIgogICAgIkkgd291bGQgbGlrZSB0byB3cml0ZSBhIHByb2dyYW0gdG8gY291bnQgdGhlIG51bWJlciBvZiBjaGFyYWN0ZXJzLCAiCiAgICAid29yZHMsIGFuZCBwYXJhZ3JhcGhzKG9yIG5ld2xpbmVzKSBpbiBhIGZpbGUuSSBoYXZlIHRoZSBjaGFyYWN0ZXJzICIKICAgICJjb3VudGluZyBjb3JyZWN0bHkgYW5kIHRoZSBuZXdsaW5lcyBjb3VudGluZyBjb3JyZWN0bHkgYnV0IEkgYW0gIgogICAgInN0dWNrIG9uIGhvdyB0byBjb3VudCB0aGUgd29yZHMsIEkgd291bGQgbGlrZSB0byBzdG9yZSB0aGUgd29yZHMgaW4gIgogICAgImFuIGFycmF5IHRvIGhvbGQgdGhlIHNpemUgb2YgdGhlIHdvcmQoaS5lLnRoZSBudW1iZXIgb2YgbGV0dGVycyAiCiAgICAiaW5mbHVlbmNlcyB3aGVyZSBpdHMgc3RvcmVkIGluIHRoZSBhcnJheSkgdGhlbiBJIG5lZWQgdG8gc3VtIHVwIHRoZSAiCiAgICAiYXJyYXkgYW5kIGRpc3BsYXkgdGhlIG91dHB1dC5cbiIKICAgICJcbiIKICAgICJUaGFua3MgaW4gYWR2YW5jZSBmb3IgdGhlIGhlbHAuIgopOwoKaW50IG1haW4oKQp7CiAgICBzdGQ6OnNpemVfdCBjaGFyYWN0ZXJzID0gMDsKICAgIHN0ZDo6c2l6ZV90IHdvcmRzID0gMDsKICAgIHN0ZDo6c2l6ZV90IHBhcmFncmFwaHMgPSAwOwoKICAgIGJvb2wgaW5fd29yZCA9IGZhbHNlOwogICAgYm9vbCBpbl9wYXJhZ3JhcGggPSBmYWxzZTsKCiAgICBjaGFyIHRva2VuOwogICAgd2hpbGUgKGluLmdldCh0b2tlbikpCiAgICB7CiAgICAgICAgaWYgKHN0ZDo6aXNzcGFjZSh0b2tlbikpCiAgICAgICAgeyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLyB3aGl0ZXNwYWNlCiAgICAgICAgICAgIGluX3dvcmQgPSBmYWxzZTsKCiAgICAgICAgICAgIGlmICh0b2tlbiA9PSAnXG4nKQogICAgICAgICAgICAgICAgaW5fcGFyYWdyYXBoID0gZmFsc2U7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vIG5vbi13aGl0ZXNwYWNlCiAgICAgICAgICAgICsrY2hhcmFjdGVyczsKCiAgICAgICAgICAgIGlmICghaW5fcGFyYWdyYXBoKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpbl9wYXJhZ3JhcGggPSB0cnVlOwogICAgICAgICAgICAgICAgKytwYXJhZ3JhcGhzOwogICAgICAgICAgICB9CgogICAgICAgICAgICBpZiAoIWluX3dvcmQpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGluX3dvcmQgPSB0cnVlOwogICAgICAgICAgICAgICAgKyt3b3JkczsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIAogICAgc3RkOjpjb3V0IDw8ICJDaGFyYWN0ZXJzOiIgPDwgc3RkOjpzZXR3KDUpIDw8IGNoYXJhY3RlcnMgPDwgJ1xuJzsKICAgIHN0ZDo6Y291dCA8PCAiV29yZHM6ICAgICAiIDw8IHN0ZDo6c2V0dyg1KSA8PCB3b3JkcyAgICAgIDw8ICdcbic7CiAgICBzdGQ6OmNvdXQgPDwgIlBhcmFncmFwaHM6IiA8PCBzdGQ6OnNldHcoNSkgPDwgcGFyYWdyYXBocyA8PCAnXG4nOwp9