Counting Words and Letters in c++
Not what you're looking for?
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;
}
}
}
Purchase this Solution
Solution Summary
A C++ Program is written to count words and letters in a given text.
Purchase this Solution
Free BrainMass Quizzes
Javscript Basics
Quiz on basics of javascript programming language.
Basic UNIX commands
Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.
C# variables and classes
This quiz contains questions about C# classes and variables.
Basic Networking Questions
This quiz consists of some basic networking questions.
Word 2010: Table of Contents
Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.