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
#include <iostream>
#include <cstring>
#include <cctype>
int main() {
constexpr size_t maxchar {1000};
constexprconstchar* term {"%%%"};
constexprconstchar* 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):
%%%
#include <iostream>
usingnamespace 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++;
} elseif (str[i] == '?') {
setence++;
} elseif (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?
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.