Please provide commenting of code so I can understand how to solve this problem. Please provide text files. Using C++ Write a program that reads a given file, and then outputs the contents of it to another file. It should also print out the number of lines and the number of times each alphabetic character appears (regardless of case) in the input file at the end of the output file. It should prompt the user for the input and output file names. For example, if the input file contains: This is just a test. Some more text is needed. Then the output file should contain: This is just a test. Some more text is needed. The number of lines in the input file is 3. A appears 1 times. D appears 2 times. E appears 6 times. I appears 3 times. M appears 1 times. N appears 1 times. O appears 2 times. R appears 1 times. S appears 6 times. T appears 6 times. U appears 1 times. X appears 1 times.
CODE:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
fstream fileIn,fileOut;
//num stores the number of line
int num = 0;
string filename,line;
string *lines = new string[100];
//opening the input file
cout<<"Enter input filename: "<<endl;
cin>>filename;
fileIn.open(filename.c_str(),ios::in);
//opening the output file
cout<<"Enter output filename: "<<endl;
cin>>filename;
fileOut.open(filename.c_str(),ios::out);
//getting the lines from a file and storing in an array
while(getline(fileIn,line)){
lines[num] = line;
fileOut<<line<<endl;;
num += 1;
}
//array to store the frequency of each alphabet
int *freq = new int[26];
for(int i=0;i<26;i++)
freq[i] = 0;
//counting the number of characters
for(int i=0;i<num;i++){
for(int j=0;j<lines[i].length();j++){
if(int(lines[i].at(j)) > 64 && int(lines[i].at(j)) <
91)
freq[int(lines[i].at(j))-65] += 1;
if(int(lines[i].at(j)) > 96 && int(lines[i].at(j)) <
123)
freq[int(lines[i].at(j))-97] += 1;
}
}
//writing the number of lines in the file
fileOut<<"Number of lines in the file is
"<<num<<"."<<endl;
//printing the frequency of each character in the file
for(int i =0;i<26;i++){
if(freq[i] != 0){
fileOut<<char(i+65)<<" appears
"<<freq[i]<<" times."<<endl;
}
}
//closing the files
fileIn.close();
fileOut.close();
cout<<"Files written!"<<endl;
return 0;
}
________________________________________
CODE IMAGES:
_________________________________________________
OUTPUT:
output.txt
_____________________________________
input.txt
This is just a test.
Please provide commenting of code so I can understand how to solve
this problem.
Please provide text files.
Using C++ Write a program that reads a given file, and then outputs
the contents of it to another file.
It should also print out the number of lines and the number of
times each alphabetic character appears (regardless of case) in the
input file at the end of the output file.
It should prompt the user for the input and output file
names.
//input file is just the question you posted..This code will work for any kind of input text.
________________________________________________
Feel free to ask any questions in the comments section
Thank You!
Get Answers For Free
Most questions answered within 1 hours.