LEX program to count the number of vowels and consonants in a given string

Prerequisite: Flex (Fast lexical Analyzer Generator)

Given a string containing both vowels and consonants, write a LEX program to count the number of vowels and consonants in given string.


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

Approach is very simple. If any vowel is found increase vowel counter, if consonant is found increase consonant counter otherwise do nothing.

Below is the implementation:





    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:");
    printf("Number of vowels are:  %d\n", vow_count);
    printf("Number of consonants are:  %d\n", const_count);
    return 0;



Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ankita_chandra

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.