#include using namespace std; string sentence; int wordCount = 0; // This assignment is worth 4 points. int main() { cout << "Enter sentence: "; getline(cin, sentence); for(int i = 0; i < sentence.length() - 1; i++) { /* Check whether if a space, comma, or period is followed by a * letter or a number. This indicates a new word present. Use * length() - 1 as the loop limit because this method means * there is no need to check the last character of the string. */ if( (sentence[i] == ' ' || sentence[i] == '.' || sentence[i] == ',') && (sentence[i+1] >= 'a' && sentence[i+1] <= 'z' || sentence[i+1] >= 'A' && sentence[i+1] <= 'Z' || sentence[i+1] >= '0' && sentence[i+1] <= '9') ) wordCount++; } // Also check if the very first character is a letter or number. if(sentence[0] >= 'a' && sentence[0] <= 'z' || sentence[0] >= 'A' && sentence[0] <= 'Z' || sentence[0] >= '0' && sentence[0] <= '9') wordCount++; cout << "Word count: " << wordCount << endl; return 0; }