How to count number of words and lines in lex program

Learn with example How to count the number of words and lines in lex program.

Logic: 

HOW TO COUNT NUMBER OF WORDS :

To count the number of words in a given input we need to calculate the number of spaces For example if an input is given as My name is Albert Dump then the output will be 5 Words but spaces are 4 so we need to add +1 to counted spaces.

HOW TO COUNT NUMBER OF LINES:

To count the number of line logic is simple if a new line comes the /n is encountered so if /n occur then we need increment count_line variable.

I hope logic is clear to you but if you have any doubt then paste in a comment.

PROGRAM: How to count number of words and lines in lex program

%{
#include<stdio.h>	
  int word, count_lines;
%}
%%
[\t " "] {word++;}
\n {count_lines++;}
%%
main(){
  yylex();
  printf("number of Words You entered ",words);
  printf("number of Lines You entered",count_lines)
  
}

PEOPLE ARE ALSO VIEWING
HOW TO MAKE CIRCLE IN C++ USING OPENGL
TOP SECRET WHATSAPP TRICKS YOU NEVER KNEW 2017

 

Leave a Reply

Your email address will not be published. Required fields are marked *