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
PLEASE ANSWER ASAP! THANKSSSSS
tankabanditka [31]
That is hard .....……
5 0
2 years ago
The highlighted items in the webpage are called …………………………………..
seraphim [82]

Answer:

highlighted items in webpage are called links.

5 0
3 years ago
What is every video game, at its root, based on?
Artist 52 [7]
Longhand drafts, good luck!
6 0
2 years ago
Read 2 more answers
You are an inexperienced excel user and don't know how to create your own formulas. you want to use one of the preconfigured for
Evgen [1.6K]

The answer is : Click the Insert Formula icon on the Formulas tab ribbon. The formula bar is located just below the Ribbon. It displays the contents of the active cell. It can also be used for entering or editing data and formulas. Found on the left of the formula bar, the insert icon dialog box helps the user identify and implement functions, a type of formula that performs specialized and group calculations.

4 0
3 years ago
Read 2 more answers
Write a program that creates a linked list object of 10 characters and creates a second list object containing a copy of the fir
slamgirl [31]

Answer:

Below code is in c++ with explanation.

Explanation:

Explanation is in comment format, any line starting with //.

#include <iostream>

#include <cstdlib>

using namespace std;

//creating a Node struct which holds node data

struct Node {  

  char data;  

  struct Node *next;  

};  

//creating variable of Node as head

// this head is the start point of ordered Linked list

struct Node* head = NULL;    

// This method is used to insert new value in node

void insert(char new_data) {  

// allocating the memory for new node

  struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));  

// assigning the data to new node

  new_node->data = new_data;  

// If head == null then it means we are inserting first value

// it will return after creating first node and assign it to

// head as head is the entry point

  if(head == NULL){

    new_node->next=NULL;

   head=new_node;

    return;

  }

// Now we need to treverse to end of linked list

// that's why creating a lastNode variable  

// As we don't want our head to point any other node

// except first node

  Node* lastNode = head;

  while(lastNode->next != NULL){

   lastNode=lastNode->next;

  }

// as last node next will be null to end the traversing  

// on linked list

  new_node->next= NULL;

// assigning last node to its previous node  

  lastNode->next = new_node;  

   

}  

// This variable will hold the first node for reverse linklist

struct Node* reverseHead = NULL;    

// This method will reverse the ordered linklist

void reversLinkList(){

  Node* lastNode = head;

// traversing the ordered linklist

  while(lastNode!= NULL){

// creating new linked list

   struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));  

   new_node->data = lastNode->data;  

   new_node->next = reverseHead;  

   reverseHead = new_node;  

   lastNode = lastNode->next;

  }

   

}

// display method will travers the linklist that

// was passed throw paramter

void display(Node* node) {  

  struct Node* ptr;

  ptr = node;

  while (ptr != NULL) {  

     cout<< ptr->data <<" ";  

     ptr = ptr->next;  

  }  

}  

// main method is the entry point of this program

int main() {  

// adding 10 nodes by calling insert method

  insert('a');

  insert('b');

  insert('c');

  insert('d');

  insert('e');

  insert('f');

  insert('g');

  insert('h');

  insert('i');

  insert('j');

  cout<<"The Ordered linked list is: ";

// calling display method with head to print ordered linklist

  display(head);  

  cout<<endl;

  cout<<"The Reversed linked list is: ";

// calling reversLinkList to reverse the ordered linklist

  reversLinkList();

// calling display method with reverseHead to display  

// reversed linklist

  display(reverseHead);

   

  return 0;  

}

4 0
3 years ago
Other questions:
  • Define and test a function myRange. This function should behave like Python’s standard range function, with the required and opt
    13·2 answers
  • Sorting table rows is based on the data in the selected        A. cell range.   B. cell.   C. column.   D. row.
    12·2 answers
  • A company currently uses Microsoft Active Directory as its identity provider. The company recently purchased Oracle Cloud Infras
    13·1 answer
  • 1What kind of rules protect everyone’s rights when we use each other’s content
    12·1 answer
  • What processes can move a solute against its concentration gradient?
    15·1 answer
  • When browsing using certain browsers, if a page is known to be malicious or using phishing techniques in the past a browser may
    5·1 answer
  • Using a text editor, create a file that contains a list of at least 15 six-digit account numbers. Read in each account number an
    7·1 answer
  • Which of the following lines of code correctly defines a function called combine? OA define "combine(a, b, c)" as: B. define(com
    9·1 answer
  • Pls help I will give points
    11·1 answer
  • What are the third generation of computer?​
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!