Pre-requisite: File Handling in C
Given a file containing some text, and two strings wordToBeFind and wordToBeReplacedWith, the task is to find all occurrences of the given word ‘wordToBeFind’ in the file and replace them with the given word ‘wordToBeReplacedWith’.
Input : File = "xxforxx xx for xx", wordToBeFind = "xx", wordToBeReplacedWith = "geeks" Output : geeksforgeeks geeks for geeks
Approach: The idea here is to read the contents from the given file, process the find and replace, and then store the output in another file.
- Make objeccts of FILE (ifp and ofp)
- Open two files, one for file input in read mode and another in write+ mode
- Check for the file to be opened correctly
- Read the contents of the existng input file word by word
- As using fgets takes the input of new line character(i.e. enter key) also we just copy the null character of the string one position back so that the newline is replaced with “\0”
- We run a loop till the end of file is reached and scan each word in the file and store it in a variable “read”.
- Then we compare “read” with “wordToBeFind” and if the result is true, we use “strcpy()” to replace “read” with “wordToBeReplacedWith”.
- Show the word replacement through printf
- Now again we shift the filepointer at the beginning of the file and print the file contents of the output file.
Below is the implementation of the above approach:
How to execute the above code:
- Copy the source code from here and paste it in an offline IDE
- Save the program.
- Create a file named “file_search_input.txt” and save it in the folder where you saved the above-copied program.
- Now open the terminal or offline IDE and run the program
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.
- Lex code to replace a word with another word in a file
- C program to Replace a word in a text by another given word
- Program to replace a word with asterisks in a sentence
- Lex program to search a word in a file
- Program to find the Encrypted word
- Lex program to find the length of the longest word
- Program to find Smallest and Largest Word in a String
- Longest Common Prefix using Word by Word Matching
- Program for length of the longest word in a sentence
- Program to print the Alphabets of a Given Word using * pattern
- Java program to count the characters in each word in a given sentence
- Find the first repeated word in a string
- Find the word with most anagrams in a given sentence
- Find the first maximum length even word from a string
- Find frequency of each word in a string in Python
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Find largest word in dictionary by deleting some characters of given string
- Find shortest unique prefix for every word in a given list | Set 2 (Using Sorting)
- Fibonacci Word
- Tribonacci Word
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.