In: Computer Science
You are given a text file containing a short text. Write a program that
1. Reads a given text file : shortText.txt
2. Display the text as it is
3. Prints the number of lines
4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same].
5. Prints the total number of special characters appear in the text.
6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt
write it in C++ programing Language
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// shortText.txt (input file)
A computer is a device that can be instructed to carry out
an
arbitrary set of arithmetic or logical operations
automatically.
The ability of computers to follow a sequence of operations
called a program make CAT computers very flexible and useful.
Since ancient times simple CAT manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.
======================================
#include <iostream>
#include <map>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>
using namespace std;
// Function Declarations
void readFile(string infile,int letters[],int &linesCount,int
&special);
void display(int letters[],int linesCount,int special);
int convertToIndex(char c);
int main() {
const int SIZE=26;
// Declaring variables
int letters[SIZE]={0};
string infile;
int linesCount=0,special=0;
//defines an input stream for the data file
ifstream dataIn;
//Getting the file name entered by the user
cout << "Enter name of the file :";
cin >> infile;
//calling the functions
readFile(infile, letters,linesCount,special);
display(letters,linesCount,special);
return 0;
}
void readFile(string infile,int letters[],int &linesCount,int
&special) {
string line;
char ch, c;
int index;
// int letter=0,totChars=0,totAlpha=0;
//defines an input stream for the data file
ifstream dataIn;
//Opening the input file
dataIn.open(infile.c_str());
//checking whether the file name is valid or not
if (dataIn.fail()) {
cout << "'" << infile << "' File Not Found
**";
exit(0);
} else {
//counting how many words in the file
while (!dataIn.eof()) {
getline(dataIn, line);
cout<<line<<endl;
linesCount++;
for (int i = 0; i < line.length(); i++) {
ch = line[i];
if (isalpha(ch)) {
c = toupper(ch);
index=convertToIndex(c);
letters[index]++;
}
else if(!isalpha(ch) && !isdigit(ch))
{
special++;
}
}
}
dataIn.close();
}
}
void display(int letters[],int linesCount,int special)
{
ofstream dataOut;
dataOut.open("occurencesText.txt");
int min=letters[0];
int max=letters[0];
int minIndx=0,maxIndx=0;
cout << "Letter Frequencies in the file are " <<
endl;
cout << "\nLetter\t\tCount" << endl;
cout << "------\t\t-----" << endl;
dataOut << "Letter Frequencies in the file are " <<
endl;
dataOut << "\nLetter\t\tCount" << endl;
dataOut << "------\t\t-----" << endl;
for(int i=0;i<26;i++)
{
if(letters[i]!=0)
{
if(min>letters[i])
{
min=letters[i];
minIndx=i;
}
if(max<letters[i])
{
max=letters[i];
maxIndx=i;
}
cout<<(char)(65+i)<<"\t\t"<<letters[i]<<endl;
dataOut<<(char)(65+i)<<"\t\t"<<letters[i]<<endl;
}
}
dataOut<<"No of lines :"<<linesCount<<endl;
dataOut<<"Nof Special Characters
:"<<special<<endl;
cout<<"No of lines :"<<linesCount<<endl;
cout<<"Nof Special Characters
:"<<special<<endl;
dataOut.close();
}
int convertToIndex(char c)
{
return (c-65);
}
=======================================
Output::
=========================================
// occurencesText.txt (output file)
=====================Could you plz rate me well.Thank You