Problem counting words individually

closed account (N7960pDG)
Can someone please help me. Thank you for looking. It is not counting the each word separately


Prompt. Input chars. Consider a period, '.', to be a sentinel: as usual with a sentinel,
when your input loop encounters the period, end the loop, and don't count the period in with your statistics.
Output the following 4 statistics, with explanatory text:

The number of digits -isdigits
The number of upper case letters - isupper
The number of "words" (words??)
The length of the longest "word" (0 if there were no words) (length of words)

Consider a "word" to be a maximal sequence of letters.
(When I say maximal, I mean that if the user types
I fly.
then we say that the number of words is 2 ("I", "fly"), not 7 ("I", "f", "fl", "fly", "l", "ly", "y"). "fl" is
not a maximal sequence of letters because it can be extended to a larger sequence ("fly").)
You may assume without checking that there will be no input failure.

An example run of your program might go as
text: 1 fish, 2 fish, red fish, blue fish.
#digits: 2
#upper: 0
#words: 6
longest word length: 4
Another example of your program might go as
text: 45AbC6dEFg-w.
#digits: 3
#upper: 4
#words: 3
longest word length: 4
(In this example, the 3 words were "AbC" and "dEFg" and "w".)

Another example of your program might go as
text: .
#digits: 0
#upper: 0
#words: 0
longest word length: 0

(It seems to me that one sensible way to proceed would be for your program variables
to include the following 5, and to have your loop, which inputs one char at a time, update them as appropriate:
# of digits seen so far
# of upper case letters seen so far
did we just see a letter in the last char? (This bool (because it's a yes or no value) is
useful because we're counting the number of words
length of longest word seen so far
length of the current word (if we're in the middle of a word) or 0 (if we're not in the middle of a word)
)

.....................................................................................................
int main()
{
int num_words = 0;

string s;
while (getline( cin, s ))
{
bool last_was_a_letter = false;
for (char c : s)
{
if (!isalpha( c ))
{
Current is not a letter. What should I check to see if I should increment num_words?
}
last_was_a_letter = (is current a letter?)
}
Now we have gone through the entire string. What should I check to see
if there was a final word that hasn't been counted yet?


*/



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

int main(){
	const char SENTINEL = '.';
	int digits = 0, uppers = 0, words = 0;
	int thisword = 0, longest = 0;
	bool wordQ = true;
	cout << "Input chars: ";
	for (;;){
		char input;
		cin >> input;
		if (input == SENTINEL) break;
		if (isalnum(input) || input == ' '){
			if (isdigit(input)){
				digits++;
			}
			if (isupper(input)){
				uppers++;
			}
			if (wordQ == true && isalpha(input)){
				words++;
				wordQ = false;
			}
			if (wordQ == false && !(isalpha(input))){
				thisword = 0;
				wordQ = true;
			}
			if (isalpha(input)){
				thisword++;
			}
			if (thisword > longest){
				longest = thisword;
			}
		}
		else{
			;
		}
		
	} // FOR LOOP END
	cout << "# digits: " << digits << endl;
	cout << "# uppers: " << uppers << endl;
	cout << "# words:  " << words << endl;
	cout << "Longest word length: " << longest << endl;
} // MAIN END
To count the words zero a flag=0 at the beginning of the program.
This is set flag = 1, if a non-space char if found.
If a space (or period) is found and flag=1 then it means a sequence of non-spaces have ended, which means the end of a word thus increment the word count. At that point you also can compare the number of non-spaces with a max value and if greater change the value. Make sure you reset the flag=0 so any more than one space after a word are not counted as the end of a word yet another word.
Last edited on
It seems to me that one sensible way to proceed would be for your program variables
to include the following 5, and to have your loop, which inputs one char at a time, update them as appropriate:
# of digits seen so far
# of upper case letters seen so far
did we just see a letter in the last char? (This bool (because it's a yes or no value) is
useful because we're counting the number of words
length of longest word seen so far
length of the current word (if we're in the middle of a word) or 0 (if we're not in the middle of a word)

No bool required; the length of the current word is 0 for "no letter" and >0 for "yes, letter".

The formatted input cin >> input; skips leading whitespace. Use unformatted cin.get(input);
@keskiverto,
Great my version (using my suggested algorithm above) worked when as I replaced cin >> input with cin.get(input)
Last edited on
Topic archived. No new replies allowed.