C Program to Count Lines, Blank Lines, Lines Ending with Semicolon

This C Program Collect Statistics of a Source File like Total Lines, Total no. of Blank Lines, Total no. of Lines ending with Semicolon.

Here is source code of the C Program to Collect Statistics of a Source File like Total Lines, Total no. of Blank Lines, Total no. of Lines ending with Semicolon. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Collect Statistics of a Source File like Total Lines, 
  3.  * Total no. of Blank Lines, Total no. of Lines ending with Semicolon
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void main(int argc, char *argv[])    /* Command line Arguments */
  9. {
  10.     int ncount = 0, ccount = 0, scount = 0, blank = 0;
  11.     char ch;
  12.     FILE *fp;
  13.     fp = fopen(argv[1], "r");
  14.     if (fp == NULL)
  15.     {
  16.         perror("Error Occured");
  17.     }
  18.     else
  19.     {
  20.         while(1)
  21.         {
  22.             ch = fgetc(fp);
  23.             if (ch == EOF)
  24.             {
  25.                 break;
  26.             }
  27.             if (ch == 10)
  28.             {
  29.                 ncount++;
  30.                 if (ch = fgetc(fp) == '\n')
  31.                 {
  32.                     fseek(fp, -1, 1);        /* shifting offset of the file to previous position */
  33.                     blank++;
  34.                 }
  35.             }        
  36.             else if (ch == 59)
  37.             {
  38.                 scount++;
  39.             }
  40.             else if (ch == '/' || ch == '*')
  41.             { 
  42.                 ccount++;
  43.             }
  44.         }
  45.     }
  46.     printf("\nThe Total number of lines are %d", ncount);
  47.     printf("\nThe Total number of Commented lines are %d", ccount);
  48.     printf("\nThe Total number of blank lines are %d", blank);
  49.     printf("\nThe total number of lines that end with Semicolon %d", scount);
  50.     printf("\nThe length of Actual code is %d ", ncount-blank-ccount);
  51.     fclose(fp);
  52. }

$ cc file8.c
$ a.out lines.c
 
The Total number of lines are 23
The Total number of Commented lines are 6
The Total number of blank lines are 4
The total number of lines that end with Semicolon 6
The length of Actual code is 13

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.