Sunday, 24 April 2016

Lex program to count the number of words,small and capital letters, digits and special characters in a C file

/*Lex program to count the number of words, characters, blank spaces and lines in a C file.*/
 
 
%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%} 
 
%%
\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%

main(void)
{
yyin= fopen("abc.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}
 
int yywrap()
{
return(1);
} 

// file abc.txt
x program to count the number of words,small and capital letters, digits and special characters in a C file
x program to count the number of words,small and capital letters, digits and special characters in a C file



// Output of the above Program
x program to count the number of words,small and capital letters, digits and special characters in a C file
Lex program to count the number of words,small and capital letters, digits and special characters in a C file

 

1 comment:

  1. Thanks for providing this information. So Informative Blog
    I hope please keep sharing this type of blog in future.
    Word Count Software

    ReplyDelete