C program to count number of words in a file


Asked by admin @ in Computers and Technology viewed by 347 People


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.

Answered by admin @



#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;    

}


Similar Questions

C program to print number in words using switch case

Asked by admin @ in Computer Science viewed by 358 persons

C program to display the number in words using switch case where a sample input and output is given

Program to find sum of two numbers in c++

Asked by admin @ in Computer Science viewed by 351 persons

Write a program in C++ to display the sum of two numbers 20 and 30.

C++ program to add two complex numbers using class

Asked by admin @ in Computer Science viewed by 401 persons

Write a c++ program to perform addition of two complex numbers using classes​

C++ program to read a file and display its contents

Asked by admin @ in Computer Science viewed by 334 persons

Write a C++ program to read the contents of a file and display it on console.

C programing

Asked by pranav94 @ in Engineering viewed by 469 persons

Write a program to split number into digits in java

Asked by admin @ in Computers and Technology viewed by 333 persons

Write a Java program that asks the user to enter a 10-digit string as a typical U.S. telephone number, extracts the 3-digit area code, the 3-digit "exchange," and the remaining …

101 to 200 numbers in words in english

Asked by admin @ in Math viewed by 367 persons

Number name 101 to 200 of 2nd class​

What is the file extension of word document

Asked by admin @ in Computer Science viewed by 270 persons

File extension of word document is​

What is default file extension for all word documents

Asked by admin @ in Computer Science viewed by 277 persons

What is the default file extension for all word documents

Program to find greatest of three numbers in python

Asked by admin @ in Computer Science viewed by 340 persons

Write a program in python to find the greatest number among three numbers​

Write a program to find average of three numbers

Asked by admin @ in Computer Science viewed by 335 persons

Write a program to find average of three numbers.

Twin prime numbers between 1 to 100 in c

Asked by admin @ in Math viewed by 304 persons

What are the twin prime numbers between 1 and 100

Counting by twos write numbers from 491 to 505

Asked by admin @ in Math viewed by 1108 persons

Counting by twos write numbers from 491 to 505

Write a program to swap two numbers in python

Asked by admin @ in Computer Science viewed by 344 persons

Write a program is python to swap two numbers.

Hindi ginti 1 to 50 in words and numbers

Asked by admin @ in Hindi viewed by 301 persons

1 to 50 Hindi numbers in word. In Hindi language only

Most viewed questions in Computers And Technology


A customer wants to increase the storage capacity by 25gb

Asked by admin @ in Computers and Technology viewed by 4431 persons




Solid yellow line bordering your lane of traffic marks a

Asked by admin @ in Computers and Technology viewed by 608 persons



Which of the following is not a windows utility program

Asked by admin @ in Computers and Technology viewed by 593 persons


When is a wrecker considered to be an emergency vehicle

Asked by admin @ in Computers and Technology viewed by 575 persons


Which operation on a pwc requires more than idle speed

Asked by admin @ in Computers and Technology viewed by 574 persons



Which of the following statements is true of a database

Asked by admin @ in Computers and Technology viewed by 559 persons


Which of the following is not true about an entrepreneur

Asked by admin @ in Computers and Technology viewed by 537 persons


Kitchen gadgets draw a context diagram for the order system

Asked by admin @ in Computers and Technology viewed by 513 persons