Counting Words and Letters in c++

Please enhance this program.
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 <iostream>

#include <cctype>

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;

}

}

}

Attachments

Solution Summary

A C++ Program is written to count words and letters in a given text.