Question
I need help splitting the contents of this program into different functions. as requested by this project Objectives: 1. Read the contents of a data
I need help splitting the contents of this program into different functions. as requested by this project
Objectives:
1. Read the contents of a data file (mytext.dat);
2. Process the frequency of characters (a-z) that appear in the data file
3. Print the frequency of characters in the file to the screen;
4. Be able to use an ifstream object;
In this assignment you will implement a program called "charword_freq.cpp" to determine the number of words and the number of occurrences of each letterin a block of text stored in a data file called mytext.dat. Define a word to beany string of letters that is delimited at each end by either whitespace, a period, acomma, or the beginning or end of a line. You can assume that the input consistsentirely of letters, whitespace, commas and periods. When outputting thenumber of words and letters that occur in a line, be sure to count upper- andlowercase versions of a letter as the same letter. Output the letters inalphabetical order and list only those letters that do occur in the input data file.Consider the following example: Block of text in the data file: hello world ALL is great. HELLO WORLD ALL IS GREAT. hellO worlD alL iS great.
Output of your program to the screen:
15 words
6 a
3 d
6 e
3 g
3 h
3 i
15 l
6 o
6 r
3 s
3 t
3 w
Your program should be modular, meaning that you should break it up into smaller function(s). Your main program should be as small as possible and well documented.
the program below:
#include #include #include
using namespace std;
int main() { string input; //Read the text file ifstream myfile("mytext.dat"); //reads the data of the file by opening it if (myfile.is_open()) { //reading lines of the file while (getline(myfile, input)) { int count[26] = { 0 }, i, wordCount = 0; //checking cases for (i = 0; i < input.size(); i++) { if (((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z')) && (input[i + 1] == '.' || input[i + 1] == ',' || input[i + 1] == ' ')) wordCount++; if (input[i] >= 'a' && input[i] <= 'z') count[input[i] - 'a']++; if (input[i] >= 'A' && input[i] <= 'Z') count[input[i] - 'A']++; } //number of words char x = input[input.size() - 1]; if (x != '.' && x != 'z' && x != ' ') wordCount++; cout << endl << wordCount << " words" << endl; for (i = 0; i < 26; i++) { if (count[i] > 0) cout << count[i] << " " << (char)('a' + i) << endl; } } //closes file myfile.close(); } else cout << "file unable to open"; system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started