More you share, more you have!!!

More you share, more you have!!!

LEX program to count the number of characters, words, spaces and lines

program using LEX

Q.Write a program using LEX to count the number of characters, words, spaces and lines in a given input file.

Source Code->  {provided file should present in that directory or create it}

%{

int ch=0, bl=0, ln=0, wr=0;

%}

%%

[\n] {ln++;wr++;}

[\t] {bl++;wr++;}

[" "] {bl++;wr++;}

[^\n\t] {ch++;}

%%

int main()

{

FILE *fp;

char file[10];

printf("Enter the filename: ");

scanf("%s", file);

yyin=fp;

yylex();

printf("Character=%d\nBlank=%d\nLines=%d\nWords=%d", ch, bl, ln, wr);

return 0;

}

Output ->

$cat > input
Girish rao salanke


$lex p1a.l

$cc lex.yy.c –ll

$./a.out

Enter the filename: input
Character=16
Blank=2
Lines=1
Word=3


Share on Google Plus
    Blogger Comment
    Facebook Comment

1 comments:

  1. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work. read

    ReplyDelete