// File sentence.cpp. SentenceAnalyzer class definitions #include "sentence.h" // // Default constructor // Pre: none // Post: The sentence analyzer is initialized with NumWords = 0. // SentenceAnalyzer::SentenceAnalyzer(void) { NumWords = 0; } // // Function to read a sentence, to put words without punctuation // into SentenceWords, and to store the number of words in NumWords // Pre: The sentence analyzer has been initialized. // Post: SentenceWords is an array of words in an // interactively read sentence that ends in a period. // NumWords is the number of words in SentenceWords. // void SentenceAnalyzer::ReadSentence(void) { int cont = TRUE, // still in sentence? i; // index char word[MAX_LENGTH + 1]; // a word in the sentence cout << "\nType a sentence: "; i = 0; do { cin >> word; // If period at end of word, it is the last word. if (word[strlen(word) - 1] == PERIOD) cont = FALSE; // Eliminate punctuation at the end of a word if (ispunct(word[strlen(word) - 1])) word[strlen(word) - 1] = '\0'; // Store the word in the array of words strcpy(SentenceWords[i], word); i++; } while(cont); NumWords = i; cin.ignore(MAX_LINE, '\n'); } // // Function to return the number of words in the sentence // Pre: The sentence analyzer has been initialized. // Post: The number of words in the sentence was returned. // int SentenceAnalyzer::NumSentenceWords(void) const { return NumWords; } // // Function to display the words of a sentence, starting a // new line before each word in a list of split words // Pre: The sentence analyzer has been initialized. // SplitWords is an array of words. // NumSplitWords is the number of words in SplitWords. // Post: The words of the sentence were displayed with each // occurrence of a word in SplitWords on a new line. // void SentenceAnalyzer::SplitSentence( const char SplitWords[][MAX_LENGTH + 1], int NumSplitWords) const { for (int i = 0; i < NumWords; i++) { if (IsSplitWord(SentenceWords[i], SplitWords, NumSplitWords)) cout << endl; cout << SentenceWords[i] << " "; } cout << "\n\n"; } // // Function to return if a word is in an array of words or not // Pre: word is a string. // SplitWords is an array of words. // NumSplitWords is the number of words in SplitWords. // Post: TRUE was returned if word is in SplitWords, // FALSE otherwise. // boolean_t SentenceAnalyzer::IsSplitWord(const char word[], const char SplitWords[][MAX_LENGTH + 1], int NumSplitWords) const { int i = 0, // index found = FALSE; // is found? while (i < NumSplitWords && !found) if (strcmp(word, SplitWords[i]) == 0) found = TRUE; else i++; return found; }