Question

In: Computer Science

Write a C program that loops and asks you every time to enter the name of...

Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file.

The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the is_functions in ctype.h

int nletters = 0;
int nlower = 0;
int nupper = 0;
int ndigits = 0;
int npuncts = 0;
int nspaces = 0;
int nalnum = 0;//digit or letter
int nlines = 0; //count end of line chars '\n' or EOF

Solutions

Expert Solution

Code:

//including libraries required
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
   //file pointers
    FILE *fptr1, *fptr2;
   //string to store file name
   //and character to read each character from file
    char filename[20], c;
   //count to store number of each character
    int nletters = 0;
   int nlower = 0;
   int nupper = 0;
   int ndigits = 0;
   int npuncts = 0;
   int nspaces = 0;
   int nalnum = 0;
   int nlines = 0;

   //taking input from the user name of the file
    printf("Enter the filename to open for reading \n");
    scanf("%s", filename);

    // opening file
    fptr1 = fopen(filename, "r");
   //if file is empty
    if (fptr1 == NULL)
    {
        printf("Cannot open file %s \n", filename);
        exit(0);
    }

   //reading each character from file
    c = fgetc(fptr1);
    //this loop runs until file reaches the end
    while (c != EOF)
    {  
       //increasing count for every corresponding character

       if(isalpha(c)){
           nletters++;
       }
       if(isalnum(c)){
           nalnum++;
       }
       if(isdigit(c)){
           ndigits++;
       }
       if(islower(c)){
           nlower++;
       }
       if(isupper(c)){
           nupper++;
       }
       if(isspace(c)){
           nspaces++;
       }
       if(ispunct(c)){
           npuncts++;
       }
       if(c=='\n'){
           nlines++;
       }
      
        //reading next character  
        c = fgetc(fptr1);
    }
    //   increasing count for EOF
        nlines++;
        //closing the file
       fclose(fptr1);
   

    //opening new file for writing
   fptr2 = fopen("out.txt", "w");
   // printing to both file and screen
       fprintf(fptr2,"The number of Alphabets are : %d\n",nletters);
       fprintf(fptr2,"The number of Numbers are : %d\n",ndigits);
       fprintf(fptr2,"The number of spaces are : %d\n",nspaces);
       fprintf(fptr2,"The number of uppercase letters are : %d\n",nupper);
       fprintf(fptr2,"The number of lowercase letters are : %d\n",nlower);
       fprintf(fptr2,"The number of alnums are : %d\n",nalnum);
       fprintf(fptr2,"The number of newlines are : %d\n",nlines);
       fprintf(fptr2,"The number of Punctuations are : %d\n",npuncts);
      
       printf("The number of Alphabets are : %d\n",nletters);
       printf("The number of Numbers are : %d\n",ndigits);
       printf("The number of spaces are : %d\n",nspaces);
       printf("The number of uppercase letters are : %d\n",nupper);
       printf("The number of lowercase letters are : %d\n",nlower);
       printf("The number of alnums are : %d\n",nalnum);
       printf("The number of newlines are : %d\n",nlines);
       printf("The number of Punctuations are : %d\n",npuncts);
   fclose(fptr2);
   //closing file
  
    return 0;
}

Output:

File that we are going to read from:

Output:

File we wrote to:

Code Screenshot:

Code Snippet:

//including libraries required
#include <stdio.h> 
#include <stdlib.h>
#include <ctype.h>
int main() 
{ 
        //file pointers
    FILE *fptr1, *fptr2;
        //string to store file name
        //and character to read each character from file 
    char filename[20], c;
        //count to store number of each character 
    int nletters = 0;
        int nlower = 0;
        int nupper = 0;
        int ndigits = 0;
        int npuncts = 0;
        int nspaces = 0;
        int nalnum = 0;
        int nlines = 0; 
  
        //taking input from the user name of the file
    printf("Enter the filename to open for reading \n"); 
    scanf("%s", filename); 
  
    // opening file
    fptr1 = fopen(filename, "r");
        //if file is empty 
    if (fptr1 == NULL) 
    { 
        printf("Cannot open file %s \n", filename); 
        exit(0); 
    } 
  
        //reading each character from file 
    c = fgetc(fptr1); 
    //this loop runs until file reaches the end
    while (c != EOF) 
    {   
        //increasing count for every corresponding character

        if(isalpha(c)){
                nletters++;
                }
                if(isalnum(c)){
                nalnum++;
                }
                if(isdigit(c)){
                ndigits++;
                }
                if(islower(c)){
                nlower++;
                }
                if(isupper(c)){
                nupper++;
                }
                if(isspace(c)){
                nspaces++;
                }
                if(ispunct(c)){
                npuncts++;
                }
                if(c=='\n'){
                        nlines++;
                }
                 
        //reading next character    
        c = fgetc(fptr1); 
    } 
    //  increasing count for EOF
                nlines++;
                //closing the file
        fclose(fptr1);
        
   
    //opening new file for writing
        fptr2 = fopen("out.txt", "w");
        // printing to both file and screen
        fprintf(fptr2,"The number of Alphabets are : %d\n",nletters);
        fprintf(fptr2,"The number of Numbers are : %d\n",ndigits);
        fprintf(fptr2,"The number of spaces are : %d\n",nspaces);
        fprintf(fptr2,"The number of uppercase letters are : %d\n",nupper);
        fprintf(fptr2,"The number of lowercase letters are : %d\n",nlower);
        fprintf(fptr2,"The number of alnums are : %d\n",nalnum);
        fprintf(fptr2,"The number of newlines are : %d\n",nlines);
        fprintf(fptr2,"The number of Punctuations are : %d\n",npuncts);
        
        printf("The number of Alphabets are : %d\n",nletters);
        printf("The number of Numbers are : %d\n",ndigits);
        printf("The number of spaces are : %d\n",nspaces);
        printf("The number of uppercase letters are : %d\n",nupper);
        printf("The number of lowercase letters are : %d\n",nlower);
        printf("The number of alnums are : %d\n",nalnum);
        printf("The number of newlines are : %d\n",nlines);
        printf("The number of Punctuations are : %d\n",npuncts);
        fclose(fptr2);
        //closing file
         
    return 0; 
}

Related Solutions

Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Write a Python program that asks the user to enter a student's name and 8 numeric...
Write a Python program that asks the user to enter a student's name and 8 numeric tests scores (out of 100 for each test). The name will be a local variable. The program should display a letter grade for each score, and the average test score, along with the student's name. Write the following functions in the program: calc_average - this function should accept 8 test scores as arguments and return the average of the scores per student determine_grade -...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric...
PYTHON Write a program that asks the user to enter a student's name and 8 numeric assignment scores (out of 100 for each assignment). The program should output the student's name, a letter grade for each assignment score, and a cumulative average for all the assignments. Please note, there are 12 students in the class so your program will need to be able to either accept data for 12 students or loop 12 times in order to process all the...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT