count vowels and consonants lex program
%{
#include <stdio.h>
int vowel_count = 0;
int consonant_count = 0;
%}
%option noyywrap
%%
[aAeEiIoOuU] { vowel_count++; }
[A-Za-z] { consonant_count++; }
[^a-zA-Z] { /* ignore non-alphabetic characters */ }
%%
int main() {
yylex();
printf("Number of vowels: %d\n", vowel_count);
printf("Number of consonants: %d\n", consonant_count);
return 0;
}
Execution
$flex vow.lex
$ gcc lex.yy.c
$ ./a.out
this is test
Number of vowels: 3
Number of consonants: 7
Comments
Post a Comment