Write the definition of the readandcount function


Discuss the below:

Enhance this program.

Q: Write the definition of the readAndCount function. The function should read the input line, character by character, counting the number of words (a sequence of letters) and the number of occurrences of each letter.

The array to hold the number of occurrences of each letter is the parameter letterCount. Store the number of occurences of 'a' at index 0, 'b' at index 1, and so forth. Be sure to account for both upper and lowercase letters. Note that the index can be computed easily from the character using subtraction of ASCII codes (which are just the 'values' of characters in C++).

To count words you need a way of determining when you have completed reading a sequence of letters. There are a few different ways to do this.

Be sure your method of counting words counts the last one.

// **********************************************************

//

// WordLetterCount.cpp

//

// This program counts the number of words and the number

// of occurrences of each letter in a line of input.

//

// **********************************************************

#include

#include

using namespace std;

void readAndCount (int &numWords, int letterCount[]);

// Reads a line of input. Counts the words and the number

// of occurrences of each letter.

void outputLetterCounts (int letterCount[]);

// Prints the number of occurrences of each letter that

// appears in the input line.

// =========================

// main function

// =========================

int main()

{

int numWords;

int letterCount[26]; // stores the frequency of each letter

cout << endl;

cout << "Enter a line of text.." << endl << endl;

readAndCount (numWords, letterCount);

cout << endl;

cout << numWords << " words" << endl;

outputLetterCounts(letterCount);

return 0;

}

// =========================

// Function Definitions

// =========================

// --------------------------------

// ----- ENTER YOUR CODE HERE -----

// --------------------------------

// --------------------------------

// --------- END USER CODE --------

// --------------------------------

void outputLetterCounts(int letterCount[])

{

for (int i = 0; i < 26; i++)

{

if (letterCount[i] > 0)

{

cout << letterCount[i] << " " << char('a' + i) << endl;

}

}

}

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Write the definition of the readandcount function
Reference No:- TGS01937598

Now Priced at $25 (50% Discount)

Recommended (96%)

Rated (4.8/5)