Write a program that computes all of the following statistics for a file and out
ID: 675294 • Letter: W
Question
Write a program that computes all of the following statistics for a file and outputs the statistics to both the screen and to another file: The total number of occurrences of characters in the file, the total number of non-whitespace characters in the file, and the total number of occurrences of letter in the file.
Your program will take as input the location and name of the file to explore. It will then go through the file character-by-character and keep track of:
Book Problem Solving with C++ 8th edition. Question 16.
the total number of characters
the total number of non-whitespace characters. Whitespace characters for this program are space and tab and carriage return (ASCII 13) and line feed (ASCII 10)
the total number of letter characters ('a' to 'z' and 'A' to 'Z').
Your solution must include at least two new functions to count characters. You must also use the existing functions isspace() and isletter().
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int lettersCount=0;
int nonAsciiCount=0;
int charactersCount=0;
void isCharacter();
void isLetter();
void isNonAsciiCharacter();
int main() {
ifstream fin;
char filename[100];
cout<<"Enter file name: ";
cin>>filename;
fin.open(filename, ios::in);
char ch;
int number_of_lines = 0;
while (!fin.eof()) {
fin.get(ch);
cout << ch;
isCharacter(ch);
isLetter(ch);
isNonAsciiCharacter(ch);
}
cout<<"File Statistics: ";
cout<<"Characters count: "<<charactersCount<<endl;
cout<<"Letters count: "<<lettersCount<<endl;
cout<<"Non Ascii Chars count: "<<nonAsciiCount<<endl;
return 0;
}
void isCharacter() {
if((ch>='a' && ch<='z')
|| (ch>='A' && ch<='Z')
|| ch!=' ' || ch!=13 || ch!=10) {
charactersCount++;
}
}
void isLetter() {
if((ch>='a' && ch<='z')
|| (ch>='A' && ch<='Z')) {
lettersCount++;
}
}
void isNonAsciiCharacter() {
if(ch==' ' || ch==13 || ch==10) {
nonAsciiCount++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.