Lab 4 Design a class named TextAnalyzer with features to analyze a text file. Yo
ID: 3722285 • Letter: L
Question
Lab 4
Design a class named TextAnalyzer with features to analyze a text file. Your class should have the following member functions:
Constructor: will receive a file name as a parameter and read its content. Set a status member variable to tell whether or not the file was uploaded successfully.
isUploaded: returns true if the file was uploaded successfully. This status is saved in a member variable that you defined in your class.
sentenceCounter: will return how many sentences there is in the text
wordCounter: will return how many words there is in the text
properNouns: will return how many potential proper nouns were found. Proper nouns start with a capital letter.
digitsCounter: will return how many digits were found in the text
Important: use the functions for character processing that you learned in Module 6.
Your class will need at least the following member variables:
A string to hold the text. You can choose either a string or c-string type variable. Note that c-strings will need to be allocated dynamically and will require you to define a destructor to deallocate that memory space.
A status variable to flag whether or not a file was uploaded to the object. All counter functions should check this status variable before proceeding to counting. Note that trying to count on an empty text may make your program crash.
Write a main program to test your class. You don't have to turn it in. Only turn in a .cpp or .h file with your class implementation. I will use it with my own main program. Note that you MUST follow the member function names that I listed because those are the names that I will call in my program.
please tell me what else is needed for this lab.
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//code.cpp
#include<iostream>
#include<fstream>
#include <ctype.h>
using namespace std;
class TextAnalyzer{
string text;
bool uploaded;
public:
TextAnalyzer(char fileName[]){
/*creating an ifstream object*/
ifstream inputFile(fileName);
if(!inputFile){
cout<<"Input file not found!"<<endl;
uploaded=false;
}else{
string line;
/*reading line by line and appending to the text*/
while(getline(inputFile,line)){
text+=line;
}
/*setting uploaded variable to true*/
uploaded=true;
}
}
/*returns true if file has been uploaded successfully*/
bool isUploaded(){
return uploaded;
}
/*returns the number of sentences*/
int sentenceCounter(){
if(isUploaded()){
int counter=0;
/*looping through all characters*/
for(int i=0;i<text.length();i++){
char c=text[i];
/*assuming that a sentence ends in period, question mark or exclaimation mark*/
if(c=='.' || c=='?' || c=='!'){
counter++;
}
}
return counter;
}
return 0;
}
/*returns the number of words*/
int wordCounter(){
if(isUploaded()){
int counter=0;
/*looping through all characters*/
for(int i=0;i<text.length();i++){
char c=text[i];
/*assuming the words are seperated by whitespaces*/
if(c==' ' ){
counter++;
}
}
return counter;
}
return 0;
}
/*returns the number of proper nouns*/
int properNouns(){
if(isUploaded()){
int counter=0;
/*looping through all characters*/
for(int i=0;i<text.length();i++){
char c=text[i];
if(i==0){
/*checking if first character in the first word is upper case*/
if(isupper(text[0])){
counter++;
}
}
/*assuming the words are seperated by whitespaces*/
if(c==' '){
/*if a space character is found, checking if the next character is upper case, so that every words can be
considered*/
if((i+1) < text.length()){
if(isupper(text[i+1])){
counter++;
}
}
}
}
return counter;
}
return 0;
}
/*returns the number of digits*/
int digitsCounter(){
if(isUploaded()){
int counter=0;
/*looping through all characters*/
for(int i=0;i<text.length();i++){
char c=text[i];
/*checking if the current char is a digit*/
if(isdigit(c)){
counter++;
}
}
return counter;
}
return 0;
}
};
int main(){
/*creating a TextAnalyzer, reading a file data.txt*/
TextAnalyzer analyzer("data.txt");
if(analyzer.isUploaded()){
/*successfully uploaded, finding and displaying all counts*/
cout<<"File has been read successfully!"<<endl;
cout<<"Number of sentences: "<<analyzer.sentenceCounter()<<endl;
cout<<"Number of words: "<<analyzer.wordCounter()<<endl;
cout<<"Number of proper nouns: "<<analyzer.properNouns()<<endl;
cout<<"Number of digits: "<<analyzer.digitsCounter()<<endl;
}
return 0;
}
//data.txt
Ought these are balls place mrs their times add she. Taken no great widow spoke of it small. Genius use except son esteem merely her limits. Sons 123park by do make on. It do oh cottage offered cottage in written. Especially of dissimilar up attachment themselves by interested boisterous. Linen mrs seems men table. Jennings dashwood to quitting marriage bachelor in. On as conviction in of appearance7 apartments boisterous.
/*OUTPUT*/
File has been read successfully!
Number of sentences: 9
Number of words: 69
Number of proper nouns: 9
Number of digits: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.