Given a text file as input, the task is to replace a given word with another 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.
Prerequisite: Flex (Fast lexical Analyzer Generator)
As we know, yytext holds the value of current matched token, we can compare it with word to replace. if the value of yytext and word to replace are same, replace the word with another word and write it to the output file otherwise simply copy the content of input file to output file.
Input File: input.txt
Below is the implementation of above approach:
Output file: output.txt
- Lex program to search a word in a file
- Lex program to count the frequency of the given word in a file
- Lex program to find the length of the longest word
- How to Prepare a Word List for the GRE General Test
- Program to print the Alphabets of a Given Word using * pattern
- LEX code to extract HTML tags from a file
- Lex program to copy the content of one file to another file
- Why Kotlin will replace Java for Android App Development
- Three address code in Compiler
- Making a QR code for a website
- Code Injection and Mitigation with Example
- Hackathon - Think, Code, Create
- DFA in LEX code which accepts strings ending with 11
- Google Summer of Code Preparation
- How to run Python code on Google Colaboratory
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.