53.7k views
4 votes
Define a C function with the name evaluateBook. The function receives as one argument the name of the file to be processed (i.e., the book's file). So that if you pass the English version, we will see the results only for that language.

User Nikita P
by
4.7k points

1 Answer

2 votes

Answer:

See explaination for the program function code.

Step-by-step explanation:

Code.

/*

Program to read a text book and count

charcters visible ,letters and punctuations using functions

*/

#include <iostream>

#include<string>

#include<fstream>

using namespace std;

//Function prototypes

void evaluateBook(string filename);

int countCharacters(string line);

int countLetters(string line);

int countPunctuations(string line);

int main()

{

//Call function

evaluateBook("Text.txt");

return 0;

}

//Function take a string and count visible charcter count

int countCharacters(string line) {

return line.length();

}

//Function take a string and count letters count

int countLetters(string line) {

int cnt = 0;

for (int i = 0; i < line.length(); i++) {

if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {

cnt++;

}

}

return cnt;

}

//Function take a string and count punctuations count

int countPunctuations(string line) {

int cnt = 0;

for (int i = 0; i < line.length(); i++) {

if (line[i]=='\.' || line[i]=='\,' || line[i]=='\"' || line[i]=='\'') {

cnt++;

}

}

return cnt;

}

//Read file line by line and count details

void evaluateBook(string filename) {

//File path

ifstream in(filename);

//Varaibles

int visibleCharacters = 0,numOfLetters=0,numOfPunctuations=0;

//File found error check

if (!in) {

cout << "File not found!!\\";

exit(0);

}

string line;

//Read line by line

while (getline(in, line)) {

//Count details

visibleCharacters += countCharacters(line);

numOfLetters += countLetters(line);

numOfPunctuations += countPunctuations(line);

}

//isplay result

cout << "Visible charcters' count = "<<visibleCharacters << endl;

cout << "Number of letters' count = " << numOfLetters << endl;

cout << "Number of punctuations' count = " << numOfPunctuations<< endl;

}

This will produce an output of;

Visible charcters' count = 252

Number of letters' count = 203

Number of punctuations' count = 10

User Ralph Tandetzky
by
4.3k points