I’m writing a function to count the number of words in a user-inputted string of text. Currently I’ve set it up for word_count
to increment every time it hits a space or punctuation mark, but I know that’s not right as it’ll increment twice when it gets to the end of a sentence. It’ll also increment when it hits an apostrophe or hyphen within a word.
I think I need to either modify my for loop to treat a word as a sequence of characters followed by a space or punctuation mark (but not an apostrophe or hyphen), and/or somehow look ahead in the string to see if there’s another space/punctuation mark coming next, and then not increment word_count
again in that case. But I’m not sure how to do either of those things (or if there’s a better way altogether).
I’m a complete beginner and doing this exercise as part of CS50’s Week 2 Readability problem set. I don’t want the final answer — just some clues please! Thanks in advance.
int count_words(string text)
{
int length = strlen(text);
int word_count = 0;
for (int i = 0; i < length; i++)
{
if (isspace(text[i]) || ispunct(text[i])) word_count++;
}
return word_count;
}
If I input “This should be 8 words. But it isn’t.” it should return 8 words. Instead it returns 10.
Vicky Carmichael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.