Explore BrainMass
Share

C++ Count Number of Letters in a Text File

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

I need help with this program. I need to create an array of 26 components to do the letter count for the 26 letters in the alphabet and a variable for the line count. (You may wish to call these variables letterCount and lineCount respectively.) Hint: Use the plan of declaring variables, opening input and output files, initializing the variables, reading and writing the character for each character in a line, incrementing the letter count, incrementing the line count, outputting line and letter counts and, finally, closing the input and output files. (You may wish to call the program outputting line and letter counts writeTotal.) Create an ASCII (or text) file that contains text that will be used as input to your program. Call this file textinput. Have the output stored in a file called textouput. Create ifstream and ofstream objects called "infile" and "outfile" respectively. Include 4 functions for initializing, copying text, counting, and writing the results to a file, respectively.
•Initialize the initial array with 0's.
•Have the file copying text specifically be able to read a line and output that line. (You may wish to call this file copyText.)
•Whenever a nonblank character is found, it calls the function that does the counting, characterCount, to update the letter count. (Do not count blank, tab, or end-of-line characters as part of the letter count.)
•Have the characterCount function convert the letter to uppercase, find the index of the array corresponding to this letter and, if the index is valid, increment appropriate count.
•Have the main program do the following:
Declare variables. Open the input and output files.If the input file does not exist, exit the program.Open the output file. If the output file cannot open, exit the program. Initialize variables counting the lines and letters. Read the first character. Use while (not end of input file).Process the next line using the function that reads a line and outputs the line copyText. Increment the line count. (Increment the variable lineCount.) Read the next character. Output the line count and letter counts.
Use writeTotal. Close files.

© BrainMass Inc. brainmass.com October 16, 2018, 10:58 pm ad1c9bdddf
https://brainmass.com/computer-science/cpp/c-count-number-of-letters-in-a-text-file-569207

Solution Summary

The solution contains a C++ program counts the number of each letter that appears in a text input file.

$2.19
Similar Posting

Strings and Vectors 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;

}

}

}

View Full Posting Details