先决条件: Flex(快速词法分析器生成器)
给定包含元音和辅音的字符串,编写LEX程序以计算给定字符串的元音和辅音的数量。
例子:
Input: Hello everyone
Output: Number of vowels are: 6
Number of consonants are: 7
Input: This is GeeksforGeeks
Output: Number of vowels are: 7
Number of consonants are: 12
方法-
方法很简单。如果找到任何元音,请增加元音计数器;如果找到辅音,请增加辅音计数器。
下面是实现:
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
{
printf("Enter the string of vowels and consonents:");
yylex();
printf("Number of vowels are: %d\n", vow_count);
printf("Number of consonants are: %d\n", const_count);
return 0;
}
输出: