Lex program to count the number of vowels & consonants from the given input string.
Problem definition:
Write a Lex Program to recognize vowels and consonants in a given string and count them and display the result on standard output.
Structure of LEX Program:
%{ Definition section %} %% Rules section %% User Subroutine section |
Click here to learn – How to Compile & Run LEX / YACC Programs on Windows 8, 10, and 11?
Source Code – Lex program to the count number of vowels & consonants in a given string
/*Lex program to count the number of vowels & consonants from the given input string.*/ %{ #include<stdio.h> int vow=0, con=0; %} %% [ \t\n]+ ; [aeiouAEIOU]+ {vow++;} [^aeiouAEIOU] {con++;} %% int main( ) { printf("Enter some input string:\n"); yylex(); printf("Number of vowels=%d\n",vow); printf("Number of consonants=%d\n",con); } int yywrap( ) { return 1; }
Output:
maheshgh@maheshgh:~/LexYacc$ lex demo.l
maheshgh@maheshgh:~/LexYacc$ gcc lex.yy.c
maheshgh@maheshgh:~/LexYacc$ ./a.out
Enter some input string:
VTU Nidasoshi VTU Belagavi
(Note: press control+d after input)
Number of vowels=10
Number of consonants=13
Summary:
This article discusses how to Write a Lex Program to recognize vowels and consonants in a given string and count them and display the result on standard output. If you like the article, do share it with your friends.