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 best describes cost-benefit analysis?
d1i1m1o1n [39]

Answer:

A

Explanation:

7 0
3 years ago
True or false: when considering data backups, it is most important to verify that the integrity of the backup file or data is va
Alika [10]

your correct answer is true

5 0
3 years ago
HELP!' Prompt<br> In a table, what is a field?
ser-zykov [4K]

Answer:

Fields are the components that provide structure for a table. In fact, you can't have a table without fields. ... Fields in a table store the same category of data in the same data type. For example, if you have a NAME field in a table of customers, the entries for this field are all customer names and are stored as text.

3 0
3 years ago
Write a program that displays the smallest of five input values that may include duplicate values (e.g., 6, 4, 8, 6, 7). Hint: R
Tpy6a [65]

Answer:

Here the code is given as follows,

Explanation:

Code:-

import java.util.*;

class Chegg {

 

   public static void main(String args[])

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter 5 numbers");

       int arr[]=new int[5];

       int min;

       for(int i=0;i<5;i++)

       {

           arr[i]=sc.nextInt();

       

       }

       min=arr[0];

       for(int i=1;i<5;i++)

       {

           if(arr[i]<min)

           {

               min=arr[i];

           }

       }

       System.out.println(min);

   }

}

3 0
3 years ago
Can anybody answer this for me ?
VARVARA [1.3K]

how about this asset assessment, threat assessment, vulnerability assessment, risk step determines the value of each asset and prioritizes the asset based upon the would have grave consequences resulting in loss of life, serious injury, or mission How will exploiting or harming U.S. assets bring the adversaries closer to their company

hope me and sans helped :-)

4 0
4 years ago
Other questions:
  • Ann wants to create a bookmark. She should
    9·2 answers
  • When investigators find evidentiary items that aren't specified in a warrant or under probable cause what type of doctrine appli
    8·1 answer
  • Python: Write a program that asks the user how many Fibonnaci numbers to generate and then generates them.
    10·1 answer
  • The most frequently applied heuristic in problem solving is a(an) _________, which involves repeated tests for differences betwe
    13·1 answer
  • Which of the following code correctly registers a handler with a button btOK?a. btOK.setOnAction(e -&gt; System.out.println("Han
    6·1 answer
  • Which of these personal protective equipment items prevents injury from falling objects ?
    15·1 answer
  • 1. PGP encryption can be performed from the command line as well. What is the PGP command line syntax to encrypt the my-message.
    7·1 answer
  • Select the first ICMP Echo Request message sent by your computer, and expand the Internet Protocol part of the packet in the pac
    10·1 answer
  • What is the output of the code? (CHECK IMAGE)
    12·1 answer
  • Which statement best describes the computers all around us?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!