top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Wap in C to collect statistics of a source file like total lines, total no of blank lines, no of lines ending with ';'

0 votes
444 views

I want to write a c program where I can count no of line, no of blank lines, no of commented lines and no of lines ending with semicolon, please help!!!

posted May 19, 2017 by Pooja Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_STRING_LENGTH 100
int main(int argc, char **argv)
{
        if(argc<2)
        {
                printf("Provide source file name as command line argument.\n");
                exit(1);
        }
        FILE *fptr;

        fptr=fopen(argv[1],"r");
        if(fptr==NULL)
        {
                printf("Unable to open file %s\n",argv[1]);
                exit(1);
        }

        char str[MAX_STRING_LENGTH];
        int lineCount=0,emptyLineCount=0,cmntLineCount=0,semiColonLineCount=0;
        while(fgets(str,MAX_STRING_LENGTH-1, fptr)!=NULL)
        {
                lineCount++;
                if(strcmp(str,"\n")==0)
                {
                        emptyLineCount++;
                        continue;
                }
                if((str[0] == '/') && (str[1] == '/'))
                {
                        cmntLineCount++;
                        continue;
                }
                if(str[strlen(str)-1] == ';')
                {
                        semiColonLineCount++;
                }
        }
        fclose(fptr);
        printf("No. of line = %d\nNo. of blank line = %d\nNo. of commented line = %d\nNo. of lines ending with semicolon = %d\n",lineCount,emptyLineCount,cmntLineCount,semiColonLineCount);
}
answer May 22, 2017 by Chirag Gangdev
...