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
- Find the word from a given sentence having given word as prefix
- Program to replace a word with asterisks in a sentence
- C program to append content of one text file to another
- Lex program to search a word in a file
- C program to check whether the file is JPEG file or not
- Longest Common Prefix using Word by Word Matching
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Program to find Smallest and Largest Word in a String
- Find if a string starts and ends with another given string
- Program to replace every space in a string with hyphen
- Lex program to find the length of the longest word
- Program to find the Encrypted word
- Java program to count the characters in each word in a given sentence
- Program to print the Alphabets of a Given Word using * pattern
- Check if a given string can be converted to another by given possible swaps
- Replace all occurrences of pi with 3.14 in a given string
- Replace every consonant sequence with its length in the given string
- Replace all occurrences of character X with character Y in given string
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.