0% found this document useful (0 votes)
8 views2 pages

CD - Exp-3 0682

The document outlines a LEX program designed to count the number of vowels and consonants in a given string. It includes an algorithm detailing the steps for implementation, including defining rules for vowel and consonant matching, and handling input/output. The program successfully executes and displays the counts of vowels and consonants after processing the input string.

Uploaded by

chandujv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

CD - Exp-3 0682

The document outlines a LEX program designed to count the number of vowels and consonants in a given string. It includes an algorithm detailing the steps for implementation, including defining rules for vowel and consonant matching, and handling input/output. The program successfully executes and displays the counts of vowels and consonants after processing the input string.

Uploaded by

chandujv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Compiler Design Lab 2024-25

Ex. No.: 3 Date: 23-01-2025

Count the number of vowels and consonants


Aim: Write a LEX program to count the number of vowels and consonants in a given string
Algorithm:
Step 1: Start
Step 2: Include necessary header files (#include <stdio.h>) for input and output operations.
Step 3: Declare and initialize global counters: int vowels = 0; to count vowels
• int consonants = 0; to count consonants Step 4: Define Lex rules:
• Use the regular expression [aeiouAEIOU] to match vowels and increment the
vowels counter.
• Use [a-zA-Z] to match consonants and increment the consonants counter.
• Use .|\n to ignore any other characters such as spaces, punctuation, and special
symbols.
Step 5: Implement the yywrap() function to return 1, indicating the end of input.
Step 6: Write the main function:
• Print a message to prompt the user: "Enter a string: ".
• Call the function yylex() to scan and process the input. Step 7: After processing,
display the results using:
• printf("Number of vowels: %d\n", vowels);
• printf("Number of consonants: %d\n", consonants); Step 8: Return 0 to indicate
successful program execution. Step 9: Stop.

Program for Count the number of vowels and consonants:


%{
#include <stdio.h>
int vowels = 0; // To count vowels int consonants = 0; // To
count consonants
%}
%%
[aeiouAEIOU] { vowels++; } // Matches vowels (both uppercase and lowercase)
[a-zA-Z] { consonants++; } // Matches consonants
(uppercase and lowercase) .|\n { /* Ignore other characters */ }
%%
int main()
{
printf("Enter a string: ");

Janapati Venkata Sai Chandra Kousthubh - BU22CSEN0100682 Dept. of CSE, GITAM BLR
Compiler Design Lab 2024-25
yylex(); // Calls the lexer to process input
printf("Number of vowels: %d\n", vowels); printf("Number of consonants: %d\n",
consonants); return 0;
}
int yywrap()
{
return 1;
}

Result:
Program executed successfully and verified output.
Output:

Janapati Venkata Sai Chandra Kousthubh - BU22CSEN0100682 Dept. of CSE, GITAM BLR

You might also like