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 25, 2018, 9:16 am 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
See Also This Related BrainMass Solution

C++ Information Retrieval Program using Binary Tree

As different authors tend to use different vocabularies and to use common words with differing frequencies. Given an essay or other text, it is interesting to find what distinct words are used and how many times each is used.

I'm trying to produce a driver program and the information-retrieval package using ordinary binary search trees.

I really want to complete this program using the outline I'm presenting you here:
1) Create the data structure (binary search tree).

2) Ask the user for the name of a text file and open it to read.

3) Read the file, split it apart into individual words, and insert the words into the data structure. With each word will be kept a frequency count (how many times the word appears in the input), and when duplicate words are encountered, the frequency count will be increased. The same word will not be inserted twice in the tree.

4) Print the number of comparisons done and the CPU time used in part 3. (By CPU time, I mean the amount of time for which the CPU was used for processing the comparisons (reading the file, splitting & inserting words)).

5) That I can, If I wish, print out all the words in the data structure, in alphabetical
order, with their frequency counts.

6) Put everything in parts 2-5 into a do . . . while loop that will run as many
times as the user wishes. Thus the user can build the data structure with
more than one file if desired. By reading the same file twice, the user can
compare time for retrieval with the time for the original insertion. (Let me clarify here, because parts 2 to 5 will be in a "do while" loop, then I can compare time for retrieval with the time for the original insertion).

Some additional specifications I was trying to implement to the program:
* I want the input to the driver will to be a file. The program will be executed with several different files; the name of the file to be used should be requested from the user while the program is running.

* A word is defined as a sequence of letters, together with apostrophes (') and hyphens (-), provided that the apostrophe or hyphen is both immediately preceded and followed by a letter. Uppercase and lowercase letters should be regarded as the same (by translating all letters into either uppercase or lowercase, as you prefer). A word is to be truncated to its first 20 characters (that is, only 20 characters are to be stored in the data structure) but words longer than 20 characters may appear in the text. Non-alphabetic characters (such as digits, blanks, punctuation marks, control characters) may appear in the text file. The appearance of any of these terminates a word, and the next word begins only when a letter appears.

* The program should not allow to be changed at all when I change implementation of data structures later.

Final specifications for the functions to I want implemented first with binary
search trees:
=========================================================
void update(const String &word,
Search_tree< Record > &structure, int &num_comps);

postcondition: If word was not already present in structure, then word has been inserted into structure and its frequency count is 1. If word was already present in structure, then its frequency count has been increased by 1. The variable parameter num_comps is set to the number of comparisons of words done.
=========================================================

void print(const Search_tree< Record > &structure);

postcondition: All words in structure are printed at the terminal in alphabetical order together with their frequency counts.
=========================================================

View Full Posting Details