Given a file containing various text, develop a program that will do the following using flex:
1. Read input from file and write output to file.
2. Count the number of vowel and consonant in the file and print the count.
3. Count a number of floating point numbers and integer numbers and print the count.
4. Count total number of white spaces.
5. Count total number of lines in the file.
test.txt file:
This is the test line 1
This is the test line 21
5.351 5 -9
2.6
Code:
%{
int vowel_counter=0;
int consonant_counter =0;
int floating_point_counter=0;
int integer_counter=0;
int white_spaces_counter=0;
int lines_counter=0;
%}
%%
[aeiouAEIOU] {vowel_counter++;}
[a-zA-Z] {consonant_counter++;}
[0-9]+.[0-9]+ {floating_point_counter++;}
[0-9]+ {integer_counter++;}
([ ])+ {white_spaces_counter++;}
(\n) {lines_counter++;}
%%
int yywrap(){}
int main(){
//1. Read input from file and write output to file.
extern FILE *yyin, *yyout;
char filename[20]="test.txt";
yyin = fopen(filename,"r");
yyout = fopen("result.txt", "w");
yylex();
//Display the number of vowel and consonant in the file and print the count.
printf("\nThe number of vowel in the file are: %d\n", vowel_counter);
printf("The number of consonant in the file are: %d\n", consonant_counter);
//Display Count a number of floating point numbers and integer numbers and print the count.
printf("The number of floating point numbers in the file are: %d\n", floating_point_counter);
printf("The number of integer numbers in the file are: %d\n", integer_counter);
//Display Count total number of white spaces.
printf("The number of white spaces in the file are: %d\n", white_spaces_counter);
//Display Count total number of lines in the file.
printf("The number of lines in the file are: %d\n", lines_counter);
//save to the file
//save the number of vowel and consonant in the file and print the count.
fprintf(yyout,"\nThe number of vowel in the file are: %d\n", vowel_counter);
fprintf(yyout,"The number of consonant in the file are: %d\n", consonant_counter);
//save Count a number of floating point numbers and integer numbers and print the count.
fprintf(yyout,"The number of floating point numbers in the file are: %d\n", floating_point_counter);
fprintf(yyout,"The number of integer numbers in the file are: %d\n", integer_counter);
//save Count total number of white spaces.
fprintf(yyout,"The number of white spaces in the file are: %d\n", white_spaces_counter);
//save Count total number of lines in the file.
fprintf(yyout,"The number of lines in the file are: %d\n", lines_counter);
fclose(yyin);
fclose(yyout);
return 0;
}
Comments
Leave a comment