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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C program to find and replace a word in a File by another given word
- C program to Replace a word in a text by another given word
- Lex program to search a word in a file
- Lex program to count the frequency of the given word in a file
- How to Prepare a Word List for the GRE General Test
- Program to print the Alphabets of a Given Word using * pattern
- Lex program to find the length of the longest word
- LEX code to extract HTML tags from a file
- Print 12 Days Of Christmas in a few lines of code | The xmas.c code
- Need for Intermediate Code and Code Optimization
- Lex program to copy the content of one file to another file
- C program to check whether the file is JPEG file or not
- Rust vs Dart - Which is More Likely to Replace C++?
- Why Kotlin will replace Java for Android App Development
- What is Google Code-In?
- Why is it Better to Learn to Code on a Whiteboard?
- Hackathon - Think, Code, Create
- Making a QR code for a website
- Three address code in Compiler
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.