How exactly would you make a wordcount program?

Sep 22, 2021 at 4:27am
This is proving to be much more difficult than I thought. It has to count words by spaces and sentences by (?,!,.) Everything I do just keeps failing.
Sep 22, 2021 at 4:56am
What have you tried so far, and in what ways have your attempts been failing?

-Albatross
Sep 22, 2021 at 6:07am
#include <iostream>
using namespace std;

int main() {
char str[1000];

cout << "Enter a paragraph: \n";
cin.getline(str,1000);

int wordcount = 0;
int setence = 0;

for(int i = 0; str[i] != '\0'; i++)
{
if(str[i] == '.'){
setence++;

}
else if(str[i] == '?') {
setence++;

}
else if(str[i] == '!'){
setence++;
}

if (str[i] == ' ')
{
wordcount++;
}
}

if(wordcount <= 1){
cout << "There are 0 words \n";
}
else{
cout << "The number of words = " << wordcount+1 << endl;
}

cout << "The number of sentences = " << setence << endl;



return 0;
}


I got everything to work for the most part. But the teacher said I need it to stop specifically when a user types in %%% which I am not sure how to do. Do I need to redo this entire array with strings? I have it to the point where I can track sentences and words
Sep 22, 2021 at 6:08am
Sorry if this a dumb question but I've only been doing C++ for a few weeks now
Sep 22, 2021 at 11:12am
Possibly:

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
#include <iostream>
#include <cstring>
#include <cctype>

int main() {
	constexpr size_t maxchar {1000};
	constexpr const char* term {"%%%"};
	constexpr const char* wrdter {".?!"};

	for (char str[maxchar] {}; (std::cout << "Enter a paragraph (" << term << " to exit):\n") && std::cin.getline(str, maxchar) && strcmp(str, term); ) {
		auto send {str + std::strlen(str)};
		auto beg {str};

		while (std::isspace(*(send - 1))) --send;
		while (std::isspace(*beg)) ++beg;
		*send = 0;

		if (!*beg)
			std::cout << "No input\n";
		else {
			size_t wordcount {}, sentence {};

			for (; *beg; ++beg) {
				if (std::strchr(wrdter, *beg)) {
					++sentence;
					while (std::strchr(wrdter, *++beg));
					--beg;
				}

				if (std::isspace(*beg)) {
					++wordcount;
					while (std::isspace(*++beg));
					--beg;
				}
			}

			std::cout << "The number of words = " << wordcount + 1 << '\n';
			std::cout << "The number of sentences = " << sentence + 1 << '\n';
		}
	}
}



Enter a paragraph (%%% to exit):
  the  quick. brown  fox!  did  jump?  over
The number of words = 7
The number of sentences = 4
Enter a paragraph (%%% to exit):
%%%

Last edited on Sep 22, 2021 at 12:58pm
Sep 22, 2021 at 3:31pm
OP's code, formatted and put into code tags:

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
#include <iostream>
using namespace std;

int main() {
  char str[1000];

  cout << "Enter a paragraph: \n";
  cin.getline(str, 1000);

  int wordcount = 0;
  int setence = 0;

  for (int i = 0; str[i] != '\0'; i++) {
    if (str[i] == '.') {
      setence++;

    } else if (str[i] == '?') {
      setence++;

    } else if (str[i] == '!') {
      setence++;
    }

    if (str[i] == ' ') {
      wordcount++;
    }
  }

  if (wordcount <= 1) {
    cout << "There are 0 words \n";
  } else {
    cout << "The number of words = " << wordcount + 1 << endl;
  }

  cout << "The number of sentences = " << setence << endl;

  return 0;
}


Yes, adapting this code to use std::string would be more convenient. Not much would have to change at the moment, just the cin.getline(str, 1000) would change to getline(cin, str).

I'm guessing that your program needs to keep reading paragraphs and counting words and sentences until finding a paragraph whose only word is %%%, then print the totals for all paragraphs.

Have you considered putting your program's loop and the two lines of code that get input inside another loop?

-Albatross
Sep 22, 2021 at 6:28pm
As your "words" are meant to be separated by whitespace, why not just read a word at a time using std::cin >> word? Put that in a loop and increment your word count each iteration and your word problem is done.

All you have to check after that is if the word is also the end of a sentence.

Traditionally, English sentence terminators will not be found anywhere but at the end of a word. (Not in the middle or beginning.)

This means you only need to check one character in each word to see if the word also has a sentence terminator. If it does then bump your sentence count.

Boom! Done!

Hope this helps.
Topic archived. No new replies allowed.