Question

In: Computer Science

You are required to write a program to provide the statistics of a file (Number of...

You are required to write a program to provide the statistics of a file (Number of letters, number of words, number of vowels, number of special characters and number of digits.

You should implement this problem as a class and call it FileContentStats which provides statistics about the number of letters, number of words, number of vowels, number of special characters, number of lines and number of digits. All of these should be private data members.

(Should be in C++ language)

Solutions

Expert Solution

C++ program for the given problem is provided below, please comment if any doubts:

Note: output and sample file used attached at the end.

C++ Code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//The class
class FileContentStats
{

//private variables
private:
   int vowelCount;
   int lettersCount;
   int specialcount;
   int digitCount;
   int wordCount;
   int linesCount;
public:

//set the statistics
FileContentStats()
{
   vowelCount=0;
   lettersCount=0;
   specialcount=0;
   digitCount=0;
   wordCount=0;
   linesCount=0;
}
void countCharacterType(string filename)
{
    int otherChar = 0;
  
   //open the file  
   ifstream filePtr(filename);

   //read the file
   for(string fileLine; getline( filePtr, fileLine ); )
   {
       linesCount++;
       wordCount++;

    for (int itr = 0; itr < fileLine.length(); itr++) {
         
        char lineChar = fileLine[itr];
       //process each character
        if ( (lineChar >= 'a' && lineChar <= 'z') ||
              (lineChar >= 'A' && lineChar <= 'Z') ) {
            lineChar = tolower(lineChar);
           //cout<<lineChar<<endl;
            if (lineChar == 'a' || lineChar == 'e' || lineChar == 'i' ||
                lineChar == 'o' || lineChar == 'u')
                vowelCount++;
            else
                otherChar++;
        }
        else if (lineChar >= '0' && lineChar <= '9')
            digitCount++;
        else if (lineChar == ' ')
           wordCount++;
       else
            specialcount++;
    }
   }
   //set the letter count
   lettersCount=(otherChar+vowelCount);
}

//display the statistics
void display()
{
   cout << "Letters: " << lettersCount << endl;
   cout << "Words: " << wordCount << endl;
    cout << "Vowels: " << vowelCount << endl;
    cout << "Digit: " << digitCount << endl;
   cout <<"Lines: "<<linesCount <<endl;
    cout << "Special Character: " << specialcount << endl;
}
};

// Driver function.
int main()
{
   //make the class object
   FileContentStats f;
  
   //process and display the file
   f.countCharacterType("input.txt");
   f.display();
   //system("pause");
    return 0;
}

Input file used:

Output:


Related Solutions

Program in C++ **********Write a program to compute the number of collisions required in a long...
Program in C++ **********Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing and double hashing. For simplicity, only integers will be hashed and the hash function h(x) = x % D where D is the size of the table (fixed size of 1001). The simulation should continue until the quadratic hashing fails.*********
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
write a program called GradeCalculator that will calculate the grading statistics of a desired number of...
write a program called GradeCalculator that will calculate the grading statistics of a desired number of students. Your program should start out by prompting the user to enter the number of students in the classroom and the number of exam scores. Your program then prompts for each student’s name and the scores for each exam. The exam scores should be entered as a sequence of numbers separated by blank space. Your program will also check for negative scores. If any...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Write a program that copies the contents of one file to a destination file. This program...
Write a program that copies the contents of one file to a destination file. This program works by first prompting the user for the name of the source and destination files. Be sure to include all necessary error checking, including ensuring that the source file exists. Also, if available, you have to use Linux system calls and not standard C library functions in your program. Write/type your source code in your submission file/document. [Restrictions] For file handling operations, only use...
In this assignment you will write a program that encrypts a text file.  You will use the...
In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme. Ask the user for the name of the original file. Ask the user for the name of the output file. Ask the user for the encryption key, n. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array. Transpose the array.  (Exchange the rows and columns.) Write the characters from the array to an output...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should...
3. Write a Java program that loads a gallery.xml file, determines the number of photos (should be only 3) in it and prints out the number of photos in the gallery.
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT