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...
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 that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT