Question

In: Computer Science

Write a C program that, given a file named Program_2.dat as input, determines and prints the...

Write a C program that, given a file named Program_2.dat as input, determines and prints the following information:

  1. The number of characters in the file.
  2. The number of uppercase letters in the file.
  3. The number of lowercase letters in the file.
  4. The number of words in the file.
  5. The number of lines in the file.

Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt from A Tale of Two Cities shown below.

  • Your program's input and output must look as much as possible like the sample run shown below (including, but not necessarily limited to, wording, punctuation, horizontal & vertical spacing, and indentation).

Assume that the program will be tested  using gcc (GCC) 8.2.0.

Sample Run # 1

When file Program_2.dat is present in the current working directory.

        Program_2_jeffsolheim.c  14 FEB 2019

        This program reads text from Program_2.dat and determines:

                1.  The number of characters in the file.
                2.  The number of uppercase letters in the file.
                3.  The number of lowercase letters in the file.
                4.  The number of words in the file.
                5.  The number of lines in the file.

        The file Program_2.dat contains:

                631 characters

                4 uppercase letters

                471 lowercase letters

                119 words

                17 lines

Sample Run # 2

When file Program_2.dat is not present in the current working directory.

        Program_2_jeffsolheim.c  14 FEB 2019

        This program reads text from Program_2.dat and determines:

                1.  The number of characters in the file.
                2.  The number of uppercase letters in the file.
                3.  The number of lowercase letters in the file.
                4.  The number of words in the file.
                5.  The number of lines in the file.

        Error! Failed to open Program_2.dat!

Sample Input File

It was the best of times,
it was the worst of times,
it was the age of wisdom,
it was the age of foolishness,
it was the epoch of belief,
it was the epoch of incredulity,
it was the season of Light,
it was the season of Darkness,
it was the spring of hope,
it was the winter of despair,
we had everything before us,
we had nothing before us,
we were all going direct to Heaven,
we were all going direct the other way--
in short, the period was so far like the present period, that some of
its noisiest authorities insisted on its being received, for good or for
evil, in the superlative degree of comparison only.

Solutions

Expert Solution

Here is the code in c for the above question:

#include <stdio.h>

int main()
{
 FILE *fp;
 char filename[100];
 char ch;
 int linecount, wordcount, charcount;

 // Initialize counter variables
 linecount = 0;
 wordcount = 0;
 charcount = 0;

 // Prompt user to enter filename
  printf("Enter a filename :");
  gets(filename);
  
    // Open file in read-only mode
   fp = fopen(filename,"r");

   // If file opened successfully, then write the string to file
   if ( fp )
   {
        //Repeat until End Of File character is reached.        
           while ((ch=getc(fp)) != EOF) {
                  // Increment character count if NOT new line or space
                    if (ch != ' ' && ch != '\n') { ++charcount; }
                  
                  // Increment word count if new line or space character
                   if (ch == ' ' || ch == '\n') { ++wordcount; }
                   
                  // Increment line count if new line character
                   if (ch == '\n') { ++linecount; }
                   


           }

           if (charcount > 0) {
                ++linecount;
                ++wordcount;
           }
    }
   else
      {
         printf("Failed to open the file\n");
        }

    printf("Lines : %d \n", linecount);
    printf("Words : %d \n", wordcount);
    printf("Characters : %d \n", charcount);

getchar();
return(0);
}

Related Solutions

Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an...
Write a C++ program that start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For each step, include a comment in your program indicating which step you are completing in the following statement(s). The easiest way to do this is copy and paste the below...
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Write a C program which prints the environment variables to a file "sample.txt" . All variables...
Write a C program which prints the environment variables to a file "sample.txt" . All variables are followed by a newline character. example: example: inputs: envp/environ = {"E1=5","E2=9",NULL} outputs: env.txt as a string would be "E1=5\nE2=9\n".
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...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT