C Program to Count No of Lines, Blank Lines, Comments in a given Program

«
»

This is a C Program to Count No of Lines, Blank Lines, Comments in a given Program.

Problem Description

This C Program counts the no of lines, blank lines, comments in a given program.

Problem Solution

1. First count the number of lines in a file.
2. Count the number of blank lines.
3. Use the while loop for step 1-2.
4. Use another while loop to count the number of comment lines in a file.
5. Use fseek function to alter the position in the file.

advertisement
Program/Source Code

Here is source code of the C Program to count no of lines, blank lines, comments in a given program. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Count No of Lines, Blank Lines, Comments in a given Program
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main(int argc, char* argv[])
  7. {
  8.     int line_count = 0, n_o_c_l = 0, n_o_n_b_l = 0, n_o_b_l = 0, n_e_c = 0;
  9.     FILE *fp1;
  10.     char ch;
  11.     fp1 = fopen(argv[1], "r");
  12.  
  13.     while ((ch = fgetc(fp1))! = EOF)
  14.     {
  15.         if (ch  ==  '\n')
  16.         {
  17.             line_count++;
  18.         }
  19.         if (ch  ==  '\n')
  20.         {
  21.             if ((ch = fgetc(fp1))  ==  '\n')
  22.             {
  23.                 fseek(fp1, -1, 1);
  24.                 n_o_b_l++;
  25.             }
  26.         }
  27.         if (ch  ==  ';')
  28.         {
  29.             if ((ch = fgetc(fp1))  ==  '\n')
  30.             {
  31.                 fseek(fp1, -1, 1);
  32.                 n_e_c++;
  33.             }
  34.         }
  35.     }
  36.     fseek(fp1, 0, 0);
  37.     while ((ch = fgetc(fp1))! = EOF)
  38.     {
  39.         if (ch  ==  '/')
  40.         {
  41.             if ((ch = fgetc(fp1))  ==  '/')
  42.             {
  43.                 n_o_c_l++;
  44.             }
  45.         }
  46.     }
  47.     printf("Total no of lines: %d\n", line_count);
  48.     printf("Total no of comment line: %d\n", n_o_c_l);
  49.     printf("Total no of blank lines: %d\n", n_o_b_l);
  50.     printf("Total no of non blank lines: %d\n", line_count-n_o_b_l);
  51.     printf("Total no of lines end with semicolon: %d\n", n_e_c);
  52. }
Program Explanation

1. Open the file and point it to the file pointer fp1.
2. Initialize the variables line_count, n_o_c_l, n_o_n_b_l, n_o_b_l, n_e_c to zero.
3. Using while loop read the next line character and store it in the variable ch. Do this until EOF.
4. Inside the loop and using if,else statements count the number of lines in the file and store it in the variable line_count.
5. Count of number of blank lines and store it in the variable n_o_b_l.
6. Check if the variable ch is equal to ;. If it is, then increment the variable n_e_c.
7. Use another while loop to count the number of comment lines and store it the variable n_o_c_l.
8. For the number of non blank lines subtract line_count from n_o_b_l.
9. Print the variables and exit.

advertisement
Runtime Test Cases
 
Total no of lines: 204
Total no of comment line: 19
Total no of blank lines: 11
Total no of non blank lines: 193
Total no of lines end with semicolon: 66

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn