Answer:
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
void evaluateBook(string bookName);
int countCharacters(string bookLine);
int countLetters(string bookLine);
int totalPunctuations(string bookLine);
int main()
{
evaluateBook("demo-book.txt");
return 0;
}
int countCharacters(string bookLine) {
return bookLine.length();
}
int countLetters(string bookLine) {
int counter = 0;
for (int i = 0; i < bookLine.length(); i++) {
if ((bookLine[i] >= 'a' && bookLine[i] <= 'z') || (bookLine[i] >= 'A' && bookLine[i] <= 'Z')) {
counter++;
}
}
return counter;
}
int totalPunctuations(string bookLine) {
int counter = 0;
for (int i = 0; i < bookLine.length(); i++) {
if (bookLine[i]=='\.' || bookLine[i]=='\,' || bookLine[i]=='\"' || bookLine[i]=='\'') {
counter++;
}
}
return counter;
}
void evaluateBook(string bookName) {
ifstream in(bookName);
int totalVisibleCharacters = 0,totalLetters=0,numberOfPunctuations=0;
if (!in) {
cout << "File not found!!\n";
exit(0);
}
string bookLine;
while (getline(in, bookLine)) {
totalVisibleCharacters += countCharacters(bookLine);
totalLetters += countLetters(bookLine);
numberOfPunctuations += totalPunctuations(bookLine);
}
cout << "Total Visible charcters count: "<<totalVisibleCharacters << endl;
cout << "Total letters count: " << totalLetters << endl;
cout << "Total punctuations count: " << numberOfPunctuations<< endl;
}
Explanation:
- Create a function that takes a bookLine string as a parameter and returns total number of visible character.
- Create a function that takes a bookLine string as a parameter and returns total number of letters.
- Create a function that takes a bookLine string as a parameter and returns total number of punctuation.
- Inside the evaluateBook function, read the file line by line and track all the information by calling all the above created functions.
- Lastly, display the results.