1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
gavmur [86]
3 years ago
11

Write a c program to count the total number of commented characters and words in a c file taking both types of c file comments (

single line and block) into account.
Computers and Technology
2 answers:
Flura [38]3 years ago
4 0

Answer:

The C program for characters will be like the one below:

Explanation:

#include <stdio.h>

#include <stdlib.h>

int main()

{

   FILE * file;

   char path[100];

   char ch;

   int characters, words, lines;

   /* Input path of files to merge to third file */

   printf("Enter source file path: ");

   scanf("%s", path);

   /* Open source files in 'r' mode */

   file = fopen(path, "r");

   /* Check if file opened successfully */

   if (file == NULL)

   {

       printf("\nUnable to open file.\n");

       printf("Please check if file exists and you have read privilege.\n");

       exit(EXIT_FAILURE);

   }

   /*

    * Logic to count characters, words and lines.

    */

   characters = words = lines = 0;

   while ((ch = fgetc(file)) != EOF)

   {

       characters++;

       /* Check new line */

       if (ch == '\n' || ch == '\0')

           lines++;

       /* Check words */

       if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')

           words++;

   }

   /* Increment words and lines for last word */

   if (characters > 0)

   {

       words++;

       lines++;

   }

   /* Print file statistics */

   printf("\n");

   printf("Total characters = %d\n", characters);

   printf("Total words      = %d\n", words);

   printf("Total lines      = %d\n", lines);

   /* Close files to release resources */

   fclose(file);

   return 0;

}

Tanzania [10]3 years ago
3 0

#include<stdio.h>

#include<stdlib.h>

int comment1(FILE *fp)

{

   char ch;

   int count=0;

   while(fscanf(fp,"%c",&ch)!=EOF)

   {

       if(ch=='\n')

       {

           return count;

       }

       count++;

   }

   return count;

}

int comment2(FILE *fp)

{

   char ch;

   int count=0;

   while(fscanf(fp,"%c",&ch)!=EOF)

   {

       if(ch=='*')

       {

           fscanf(fp,"%c",&ch);

           if(ch=='/')

           {

               return count;

           }

           count++;

       }

       count++;

   }

   return 0;

}

int main()

{

   printf("Enter the file name:");

   char s[1000],ch,ch1;

   scanf("%s",s);

   FILE*fp;

   fp = fopen(s,"r");

   int count=0;

   while(fscanf(fp,"%c",&ch)!=EOF)

   {

       if(ch=='\"')

       {

           while(fscanf(fp,"%c",&ch)!=EOF)

           {

               if(ch=='\"')

               {

                   break;

               }

               if(ch=='\\')

               {

                   fscanf(fp,"%c",&ch);

               }

           }

       }

       else if(ch=='/')

       {

           fscanf(fp,"%c",&ch);

           if(ch=='/')

           {

               count += comment1(fp);

           }

           else if(ch=='*')

           {

               count += comment2(fp);

           }

       }

   }

   printf("%d\n",count);

   return 0;    

}

You might be interested in
Which of the following is an example of hypertext pattern reading?
Murrr4er [49]
A is the correct answer
5 0
3 years ago
Choose the types of education an ISS professional
Viefleur [7K]

Answer:

It's A. a CompTIA certificate and D. a Microsoft-certified desktop support

certificate

Explanation:

I got it right.

7 0
3 years ago
After a normal system shutdown, when the computer is turned off, contents of the memory used to store bios settings are
kiruha [24]
The settings are retained

<span>Technically, Bios software is stored in a non-volatile ROM (Read-Only Memory) chip found on the motherboard.  Usually, this kind of chip retains its contents even after a normal system shutdown. Rom chips contains programs that are collectively referred to as BIOS chip or Basic Input/Output Services</span>
8 0
3 years ago
Read 2 more answers
A user calls your help desk to report that the files on her USB stick do not have any permissions associated with them and there
Delicious77 [7]
It is (D) because FAT 32 is the most secure option that is why it is most popular
7 0
3 years ago
Read 2 more answers
Software is the brain of computer. Explain this starement
Annette [7]

here you go

i hope this helps you

5 0
3 years ago
Other questions:
  • When did Kodak introduce film photography to the commercial market?
    10·2 answers
  • That is Entrepreneur ? ?<br>​
    8·1 answer
  • You listened to a song on your computer. Did you use hardware or software? Explain.
    11·2 answers
  • The Microsoft-NanoServer-Guest-Package installs the drivers necessary for installing Nano Server as a host OS on a physical mach
    15·1 answer
  • A user is unable to install virtualization software on a Windows 8.1 computer. The user verified the host has sufficient RAM, pl
    6·1 answer
  • Data types influence the following in the workflow: (Select all that apply)a. the color choices and layout decisions around comp
    15·2 answers
  • . public members are accessible from ________________ where the object is visible
    9·1 answer
  • Who is he can anyone help me​
    14·2 answers
  • Help please!! worth 10 pts! answer quick please!
    12·1 answer
  • If you're connected to a switch and your NIC is in promiscuous mode, what traffic would you be able to capture
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!