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]
2 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]2 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]2 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
The process of arranging the item of a column in some sequence or order is known as?
MatroZZZ [7]

answer: sorting??  

(hope it helps)

6 0
2 years ago
I forgot to tell it’s on Roblox for those who play and wanted to be friends and new ppl username is mosarider489
never [62]

Answer:.

Explanation:

.

8 0
3 years ago
Read 2 more answers
Effective feedback should focus on the behavior, not the person.<br><br> True<br> False
Aleonysh [2.5K]
Your answer is False my fellow samurai
5 0
3 years ago
Read 2 more answers
WHAT DOES INFORMATION TECHNOLOGY DO??<br> Do they offer services or products
notsponge [240]
Yes

Explanation: Information technology, or IT, describes any technology that powers or enables the storage, processing and information flow within an organization. Anything involved with computers, software, networks, intranets, Web sites, servers, databases and telecommunications falls under the IT umbrella.
4 0
2 years ago
A program is required to three (3) numbers. calculate and print their total
podryga [215]

A program that is required to three (3) numbers. calculate and print their total is given below:

<h3>The Program</h3>

import java.util.Scanner;

public class SumAndAverage {

public static void main(String[ ] args) {

System.out.println("Enter three numbers - ");

// Accepting and finding sum of numbers.

int sum = 0;

Scanner sc = new Scanner(System.in);

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

sum += sc.nextInt( );

// Printing sum and average.

System.out.println("Sum - " + sum);

System.out.println("Average - " + (sum / 3f));

}

}

The output would request three different numbers, then add them up, and display the output of the sum and then display or print their total


Read more about programming here:

brainly.com/question/23275071

#SPJ1

5 0
1 year ago
Other questions:
  • Select the correct answer.
    7·2 answers
  • What is binary number
    11·1 answer
  • My speaker on my phone is not working, when I try and play music and videos the sound dosen't work. However my alarm works. Can
    9·2 answers
  • Write the header file Stadium.h for a Stadium class. The Stadium class has the following data members: 1) an array of 1000 Seat
    7·1 answer
  • What keyword must be used on any method (function) on your class that is called from your main() method?
    15·1 answer
  • Use the drop-down menus to complete the statements about changing mail options in Outlook.
    10·1 answer
  • CALLING ALL DEKUS UWU
    15·2 answers
  • Which option should you choose to change the background of your current slide?
    10·2 answers
  • I NEED HELP, DO ASAP <br> Why do you have to adjust toys for different ages?
    11·2 answers
  • Which characteristics support an agile mis infrastructure?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!