1A. Program to count the number of characters, words, spaces and lines in a given input file. [VTU SS&OS]

vtu ss&os 1a
Code:
Algorithm:
Step1: Start
Step2: Read Input file Name
Step3: Initialize wc=0 , lc=0, cc=0, bc=0
Step4: If input file content matches
           [^ \t\n]+ then increment wc=wc+1 and cc=cc+ length of the current scanned word
           else
           \n then increment lc=lc+1
           else
           “ “ then increment bc=bc+1
          else
          \t then increment bc=bc+8 and cc=cc+1
Step5: Print the number of Words, Characters, Lines, Blank Spaces in the file
Step6: Stop

Explanation:
The aim of the code is to count the number of characters, word, lines and blank spaces. Logic is simple. Declare four variables for each count and then update each variables according to the pattern matched. Lets begin.

%{
int wc=0,cc=0,lc=0,bc=0;
char infile[25];
%}

This is declaration section of Lex part. Declare four variable to hold count of each words, characters, lines and spaces. initialize to 0 at the beginning. An extra variable for storing input file name is also declared.

word    [^  \t\n]+
eol        \n

 These are two regular expression declaration for each word and End of line. (self explanatory)

%%
{word}   {wc++; cc+=yyleng;}
{eol}  {lc++; cc++; }
[   ]  {bc++; cc++; }
[\t]              {bc+=8; cc++;}
%%

'%%' represents seperation of declartion and definition section. Here we declare that if any of the pattern described in the definition section matches the scanned input in the input file, we are performing an action specified in the {...} part. We do so for each pattern. i.e., word, character,line and spaces.

  • If word is encountered, then increment wc by 1. and increment character count by the length of matched character which is by default stored in lex variable 'yyleng'.
  • If End of line 'eol' is encountered, then increment line count and character count by 1.
  • If it is a blank space '[ ]' then increment blank space count 'bc' and character count 'cc' by 1.
  • If tab space, '\t' then increment character count by 1 and blank space count by 8.
main()
{
printf(" Read the Input File Name \n ");
scanf(“%s”,infile);
yyin=fopen(infile,"r");      
yylex();
fclose(yyin);
printf(" No. of Chars: %d\n No. of Words: %d\n No.of Lines: %d\n No. of  Blanks: %d\n",cc,wc,lc,bc); 
}

This is user subroutine section. Read input file name and redirect the standardinput of lex to the gievn input file name. Then call 'yylex()' to begin pattern matching. After the calculation is done, close the file using fclose() and print all the counts. 

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

2 comments

Write comments
Unknown
AUTHOR
7 December 2016 at 10:00 delete

All the programs are clearly explained. Thankyou so much for making us understand the concepts and helping us do well in the lab exams. Great Work ! Keep Going :)

Reply
avatar

Share your views about this article!