C and C++ OS Shell Script

Program to count the number of characters words spaces and lines in a given input file

%{
#include
int c=0,s=0,w=0, l=0;
%}
%%
[ ] {s++;}
[\n] {l++;}
[^\t\n]+ {w++; c=c+yyleng;}
%%
main(int argc, char *argv[ ])
{
FILE *fp;
if(argc == 2)
{
fp = fopen(argv[1],"r");
if(!fp)
{
printf("file doesnot exist\n");
exit(0);
}
yyin = fp;
yylex();
printf("number of spaces is %d\n",s);
printf("number of character is %d\n", c);
printf("number of words is %d\n", w);
printf("number of lines is %d\n", l);
}
else
printf("enter proper number of arguments\n")
}
OUTPUT
vi pgm.l
flex pgm.l
cc lex.yy.x -lf1
vi pgm.sh
cat pgm.sh

Operating system
system software
shell script

./a.out pgm.sh
number of space is 3
number of character  is 40
number of words is 6
number of lines is 3

Leave a comment

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