user3403337
user3403337

Reputation: 55

How to get the number of words in a string

I have an assignment due for my intro c++ class next week and I need some advice for how to go about doing it. I have to make a word count program that gives out the number of lines, words, characters, unique lines, and unique words in a users list input. So far, this is what I have:

Upvotes: 0

Views: 1064

Answers (3)

Proxy
Proxy

Reputation: 1854

I'm not going to write all the code for you, but here are the algorithms:

Lines

Loop through the lines of the string using getline with newline as a delimiter. For each line (if it's not empty?), increment the number of lines.

Words

For each line, loop through the words using getline with a delimiter of space. For each line, if it's not empty, increment the number of words.

Characters

Counting spaces

std::string.size()

Not counting spaces

unsigned int numCharacters;
for (std::string::size_type i = 0; i < str.size(); i++)
    if (str[i] != ' ') numCharacters++;

Unique Lines/Words

Loop through the same way as counting the lines/words, but add each line to an std::vector -- std::vector.push_back() -- if it doesn't already contain the line.

Upvotes: -1

Jerry Coffin
Jerry Coffin

Reputation: 490148

If possible, I would put a line into a std::istringstream, and read words from there to count them. To count unique words/lines, it's almost certainly easiest to insert them into a std::set, then count the number of items in the set when you're done (which looks like pretty much what you're already attempting, so this is pretty much just a confirmation that this approach is entirely viable).

If you need to separate words without a stringstream, it's (generally) easiest to find a non-space character, then copy characters to a string until you encounter a space character again. Repeat until you reach the end of the input.

Upvotes: 2

vonbrand
vonbrand

Reputation: 11791

Simplest is probably set up a counter (words) and a flag(in_word), starting at 0 and false. Read a character at a time. Depending of in_word and if the current character is a word-character or not, change state and possibly increment the number of words (essentially count word starts).

This sounds messy, but take a line (this one will do ;-) and see how it should go.

Upvotes: 0

Related Questions