Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to fix a bit of code that is supposed to count blank spaces as well as characters. I am unsure how to

I am trying to fix a bit of code that is supposed to count blank spaces as well as characters. I am unsure how to add the correct code in and what it should look like. I have copied the code I have below.

/* * File: Lab 3 * Author: Aaron Sexton * This program */ #include #include #include #include #include

using namespace std;

// read paragraph file is cout << paragraph << endl; // constants const int MAX_WORD_CHARS = 50; // longest word = 50 chars const int MAX_WORDS = 1000; // longest paragraph = 100 words const int MAX_PARAGRAPH_CHARS = 50000; // 50 * 1000 // max number of sentences in paragraph const int MAX_SENTENCES = 100; // max number chars in a sentence; const int MAX_SENTENCE_CHARS = 500; const int NUM_TO_BE_VERBS = 5; const char TO_BE_VERBS[NUM_TO_BE_VERBS][MAX_WORD_CHARS] = { "am", "are", "is", "was", "were" }; const char BE[] = "be"; const char TO[] = "to"; const int NUM_CONJUNCTIONS = 7; // conjunctions const char CONJUNCTIONS[NUM_CONJUNCTIONS][MAX_WORD_CHARS] = {"for", "and", "nor", "but", "or", "yet", "so" }; const int NUM_PUNCTUATIONS = 4; const char PUNCTUATIONS[NUM_PUNCTUATIONS] = { '.', ',', '?', '!' };

// Function prototypes int tokenizeParagraph( char [], char [][MAX_WORD_CHARS] ); bool endsWith( const char [], char ); void countChars( const char [][MAX_WORD_CHARS], const int, int& ); void countWords( const char [][MAX_WORD_CHARS], const int, int& ); void countSentences( const char [][MAX_WORD_CHARS], const int, int& ); void countSimpleSentences(const char [][MAX_WORD_CHARS], const int, int& ); void countToBeVerbs(const char [][MAX_WORD_CHARS], const int, int& ); void averageWordsPerSentence( const int, const int, double& );

// Main int main() { // Declare variables char paragraph[MAX_PARAGRAPH_CHARS]; char tokenizedParagraph[MAX_WORDS][MAX_WORD_CHARS]; char filename[MAX_WORD_CHARS]; int numChars = 0, numWords = 0, numSentences = 0; int numSimpleSentences = 0, numToBeVerbs = 0; double avgWordsPerSentence = 0; ifstream inputFile;

// Welcome user cout << "Welcome to the English Analyzer." << endl; cout << "Enter file name: "; cin >> filename; inputFile.open(filename);

// Check if file exists if ( !inputFile ) { cout << "Input file " << filename << " does not exist." << endl << endl; } else { // Get paragraph inputFile.getline(paragraph, MAX_PARAGRAPH_CHARS); inputFile.close();

// Check if paragraph is empty if ( strlen(paragraph) == 0 ) { cout << "Input file " << filename << " is empty." << endl << endl; } else { // Tokenize paragraph int numWords = tokenizeParagraph(paragraph, tokenizedParagraph); // Count characters countChars(tokenizedParagraph, numWords, numChars); // Count words countWords(tokenizedParagraph, numWords, numWords); // Count sentences countSentences(tokenizedParagraph, numWords, numSentences); // Count simple sentences countSimpleSentences(tokenizedParagraph, numWords, numSimpleSentences); // Count "to be" verbs countToBeVerbs(tokenizedParagraph, numWords, numToBeVerbs); // Count average words per sentence averageWordsPerSentence(numWords, numSentences, avgWordsPerSentence); // Print results cout << "Number of characters: " << numChars << endl << "Number of words: " << numWords << endl << "Number of sentences: " << numSentences << endl << "Average number words in a sentence: " << fixed << setprecision(1) << avgWordsPerSentence << endl << "Number of simple sentences: " << numSimpleSentences << endl << "Number of \"to be\" verbs: " << numToBeVerbs << endl; } }

cout << "Thank you for using the English Analyzer." << endl; return 0; }

int tokenizeParagraph( char p[], char tp[][MAX_WORD_CHARS] ) { int i = 0; char* cPtr; cPtr = strtok( p, " \t"); while( cPtr != NULL ) { strcpy( tp[i], cPtr ); i++; cPtr= strtok( NULL, " \t"); } return( i ); }

/* endsWith: tests if a word ends in a particular character

Parameters:

word: the word to test

c: the character to test for at the end of the word

Pre-Condition: word is a valid C-string, c is a char

Post-Condition: none

Returns: true if the word ends with c, false otherwise

*/ bool endsWith( const char word[], char c ) { int wordLength = strlen( word );

if ( word[wordLength - 1] == c ) return true; else return false; }

/* countChars: counts the number of characters in an array of words

Parameters:

words: a 2D array of words

numWords: the number of words

numChars: the number of characters in the words array

Pre-Condition: words is a valid 2D array of words, numWords is the number of words

Post-Condition: numChars is the number of characters in the words array

Returns: nothing

//FIX TO COUNT SPACES */ void countChars( const char words[][MAX_WORD_CHARS], const int numWords, int& numChars ) { numChars = 0;

for ( int i = 0; i < numWords; i++ ) { numChars += strlen( words[i] ); } } /* countWords: counts the number of words in an array of words

Parameters:

words: a 2D array of words

numWords: the number of words

numWords: the number of words in the words array

Pre-Condition: words is a valid 2D array of words, numWords is the number of words

Post-Condition: numWords is the number of words in the words array

Returns: nothing

*/ void countWords( const char words[][MAX_WORD_CHARS], const int numWords, int& numWordsOut ) { numWordsOut = numWords; }

/* countSentences: counts the number of sentences in an array of words

Parameters:

words: a 2D array of words

numWords: the number of words

numSentences: the number of sentences in the words array

Pre-Condition: words is a valid 2D array of words, numWords is the number of words

Post-Condition: numSentences is the number of sentences in the words array

Returns: nothing

*/ void countSentences( const char words[][MAX_WORD_CHARS], const int numWords, int& numSentences ) { numSentences = 0;

for ( int i = 0; i < numWords; i++ ) { if ( endsWith( words[i], '.' ) || endsWith( words[i], '?' ) || endsWith( words[i], '!' ) ) numSentences++; } }

/* countSimpleSentences: counts the number of simple sentences in an array of words

Parameters:

words: a 2D array of words

numWords: the number of words

numSimpleSentences: the number of simple sentences in the words array

Pre-Condition: words is a valid 2D array of words, numWords is the number of words

Post-Condition: numSimpleSentences is the number of simple sentences in the words array

Returns: nothing

*/ void countSimpleSentences(const char words[][MAX_WORD_CHARS], const int numWords, int& numSimpleSentences ) { numSimpleSentences = 0;

for ( int i = 0; i < numWords - 1; i++ ) { bool isSimpleSentence = true;

for ( int j = 0; j < NUM_CONJUNCTIONS; j++ ) { if ( strcmp( words[i], CONJUNCTIONS[j] ) == 0 && strcmp(words[i + 1], BE ) == 0 ) { isSimpleSentence = false; break; } }

if ( isSimpleSentence ) { if ( endsWith( words[i], '.' ) || endsWith( words[i], '?' ) || endsWith( words[i], '!' ) ) numSimpleSentences++; } } }

/* countToBeVerbs: counts the number of "to be" verbs in an array of words

Parameters:

words: a 2D array of words

numWords: the number of words

numToBeVerbs: the number of "to be" verbs in the words array

Pre-Condition: words is a valid 2D array of words, numWords is the number of words

Post-Condition: numToBeVerbs is the number of "to be" verbs in the words array

Returns: nothing

*/ void countToBeVerbs( const char words[][MAX_WORD_CHARS], const int numWords, int& numToBeVerbs ) { numToBeVerbs = 0;

for ( int i = 0; i < numWords - 1; i++ ) { if ( strcmp( words[i], TO ) == 0 && strcmp(words[i + 1], BE ) == 0 ) { numToBeVerbs++; } else { for ( int j = 0; j < NUM_TO_BE_VERBS; j++ ) { if ( strcmp( words[i], TO_BE_VERBS[j] ) == 0 ) { numToBeVerbs++; break; } } } } }

/* averageWordsPerSentence: computes the average of words per sentence in an array of words

Parameters:

numWords: the number of words

numSentences: the number of sentences in the words array

avgWordsPerSentence: the average of words per sentence in the words array

Pre-Condition: numWords is the number of words, numSentences is the number of sentences

Post-Condition: avgWordsPerSentence is the average of words per sentence in the words array

Returns: nothing

*/ void averageWordsPerSentence( const int numWords, const int numSentences, double& avgWordsPerSentence ) { avgWordsPerSentence = ( (double) numWords ) / numSentences; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions