Question

In: Computer Science

You are given a text file containing a short text. Write a program that 1. Reads...

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

Solutions

Expert Solution

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


Related Solutions

Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT