C program to count number of words in a file


Asked by wiki @ in Computers and Technology viewed by 365 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 wiki @


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

}

Do you know the better answer?

Latest questions in Computers And Technology

Write a program to split number into digits in java

Asked by wiki @ in Computers and Technology viewed by 160 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 …

Write a program to calculate area of circle in c++

Asked by wiki @ in Computers and Technology viewed by 114 persons


I want to write a C++ program to read the diameter of a circle. The program should find and display the area and circumference of the circle. Define a constant …

Write a c++ program to calculate gpa in a semester

Asked by wiki @ in Computers and Technology viewed by 232 persons


Write a C++ program that opens and reads file question2.txt. File question1.txt has 3 students and 3 grades of each. You program should read values from file and assign values …

Para que sirve el programa dev c++

Asked by wiki @ in Tecnología y Electrónica viewed by 206 persons


Para q me sirve el dev-c++?

Programa para adivinar un numero en c++

Asked by wiki @ in Tecnología y Electrónica viewed by 171 persons


Necesito ayuda con un codigo de Dev c++ , este programa es un juego de adivinar el numero de 1 a 30 con 5 intentos por alguna razon no me …

C++ program to perform arithmetic operations using objects and classes

Asked by wiki @ in General viewed by 229 persons


How do you write a C++ program to find the arithmetic operations on complex numbers using operator overloading using the C++ language?

Programa para calcular el area de un triangulo en c++

Asked by wiki @ in Informática viewed by 160 persons


: Programa que calcule el área de un triángulo en c++ es para mañana

Programa en c para convertir un numero decimal a binario

Asked by wiki @ in Tecnología y Electrónica viewed by 108 persons


Convertir el sistema decimal a binario, los siguientes números: a) 32 b) 147 c) 43 d) 80 e) 7512 f) 145 g) 3587 h) 1542 i) 8641 es para las …

Programa en c para calcular el area de un triangulo

Asked by wiki @ in Informática viewed by 156 persons


: Programa que calcule el área de un triángulo en c++ es para mañana

Programa para saber si un numero es primo en c++

Asked by wiki @ in Informática viewed by 111 persons


Necesito un código en c++ que imprima los 100 primeros números primos sin usar "for" por favor lo necesito

Programa que calcule la potencia de un numero en c++

Asked by wiki @ in Informática viewed by 155 persons


C++ programa que calcule la potencia ingresada del número ingresado por el usuario, es decir se deberá solicitar el número que se desee elevar a la potencia ingresada por el …

Somebody hit me.

Asked by solange_darlhene @ in World Languages viewed by 36 persons

What is the main idea and supporting ideas of the text?

Asked by leidy214 @ in Biología viewed by 115 persons


Más de la mitad de la población mundial se concentra en las ciudades y esta concentración se convierte en una amenaza. El acelerado desarrollo urbano genera una serie de problemas, …

Main idea secondary ideas

Asked by leidy214 @ in Biología viewed by 54 persons


Más de la mitad de la población mundial se concentra en las ciudades y esta concentración se convierte en una amenaza. El acelerado desarrollo urbano genera una serie de problemas, …

Most viewed questions in Computers And Technology


Which of the following is a valid c++ array definition

Asked by wiki @ in Computers and Technology viewed by 433 persons

Which of the following is a valid C++ array definition? A. int array[0]; B. float $payments[10]; C. void numbers[5]; D. …

Which of the following is not a method for authentication

Asked by wiki @ in Computers and Technology viewed by 430 persons

Which of the following are NOT used for user authentication? A. what the user has B. what the user knows …

How do we measure the frequency of a sound wave

Asked by wiki @ in Computers and Technology viewed by 389 persons

What two units are used to measure the frequency and amplitude of a sound wave? A. Hertz is a measure …


C program to count number of words in a file

Asked by wiki @ in Computers and Technology viewed by 365 persons

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

Which of the following is not a data mining functionality

Asked by wiki @ in Computers and Technology viewed by 363 persons

Define each of the following data mining functionalities: characterization, discrimination, association and correlation analysis, classification, prediction, clustering, and evolution analysis. …

Square root of a number by newton's method in python

Asked by wiki @ in Computers and Technology viewed by 361 persons

Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of …


How to get access to advanced server in mobile legends

Asked by wiki @ in Computers and Technology viewed by 336 persons

How do you get access To The advance server in Mobile legends

An advantage of electronic data interchange edi is that it

Asked by wiki @ in Computers and Technology viewed by 325 persons

The advantages of electronic data interchange are no longer limited to large companies, because even small firms can exchange and …

Which of the following statements is true about learning disability

Asked by wiki @ in Computers and Technology viewed by 321 persons

Which of the following statements are true regarding cognitive impairments? Select 3 options. To improve cognitive accessibility, the developer should …


Which of the following software programs provides for email communication

Asked by wiki @ in Computers and Technology viewed by 316 persons

Which of the following software programs provides for e-mail communication?. A. Access. B. Word Perfect. C. Outlook. D. Excel

How to know if someone read your message on android

Asked by wiki @ in Computers and Technology viewed by 260 persons

How to know if someone read your message android?

Which of the following is recommended to avoid malware infections

Asked by wiki @ in Computers and Technology viewed by 256 persons

Which of the following statements about malware protection are accurate? Select 3 options. Firewalls and anti-malware should be used together …


How do you make a division sign on a computer

Asked by wiki @ in Computers and Technology viewed by 253 persons

How do I make a division sign with my Chromebook keyboard?

Checking a credit report is a good way to brainly

Asked by wiki @ in Computers and Technology viewed by 246 persons

If your credit reports show different scores, what should you do for everfi

What are some the benefits of the agile development methodology

Asked by wiki @ in Computers and Technology viewed by 246 persons

What are some benefits of the Agile development methodology? a)It allows people to contribute to more than one part of …