Respuesta :

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

}

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;

}