Problem: Given a text file as input, the task is to count frequency of a given word in the file.
Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.
As we know, yytext holds the value of current matched token, we can compare it with the word whose frequency is to be counted. if the value of yytext and the given word are same, increment the count variable.
Input File: input.txt
Below is the implementation of above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Lex program to search a word in a file
- Lex code to replace a word with another word in a file
- Lex program to copy the content of one file to another file
- Lex program to find the length of the longest word
- LEX program to add line numbers to a given file
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Lex Program to print the total characters, white spaces, tabs in the given input file
- Lex Program to count number of words
- Lex program to count words that are less than 10 and greater than 5
- Lex program to count the number of lines, spaces and tabs
- Lex Program to Identify and Count Positive and Negative Numbers
- LEX program to count the number of vowels and consonants in a given string
- Lex program to Count the Positive numbers, Negative numbers and Fractions
- Frequency Reduction in Code Optimization
- LEX code to extract HTML tags from a file
- Lex Program to remove comments from C program
- C program to detect tokens in a C program
- Lex code to count total number of tokens
- Lex program to identify the identifier
- Lex Program For checking a valid URL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.