How it works...

Open the chosen file in read-only mode. If the file opens successfully, then the file pointer, fp, will be set to point at it. Enter the word to be replaced and assign it to the string variable, str1. Similarly, enter the new string that will be assigned to another string variable, str2. The length of the two strings, str1 and str2, will be computed and assigned to the variables, ls1 and ls2, respectively.

Set a while loop to execute until the file pointed at by fp pointer gets over. Within the while loop, one line from the file will be read using the fgets function. The fgets function reads the file until the maximum length that is specified or the new line character, \n, is reached, whichever comes first. Because strings are terminated with a mandatory null character, \0, a maximum of 254 characters will be read from the file.

The string that is read from the file will be assigned to the line variable. The length of the line string will be computed and assigned to the ll variable. Using a for loop, each of the characters in the line variable will be accessed to check whether they match with str1[0]—that is, with the first character of the string to be replaced. The characters in the line variable that don't match with the string to be replaced will be assigned to another string, called nline. The nline string will contain the desired content—that is, all of the characters of the line variable and the new string. If it exists in line, then the string will be replaced with the new string and the entire modified content will be assigned to the new string, nline.

If the first character of the string to be replaced matches with any of the characters in line, then the while loop will be used to match all of the successive characters of the string that is to be replaced with the successive characters in line. If all of the characters of the string that is to be replaced match with successive characters in line, then all of the characters of the string to be replaced are replaced with the new string and assigned to the new string, nline. That way, the while loop will read one line of text at a time from the file, searching for occurrences of the string to be replaced. If it is found, it replaces it with the new string and assigns the modified line of text to another string, nline. The null character, \0, is added to the modified string, nline, and is displayed on the screen. Finally, the file pointed to by the file pointer, fp, is closed.

In this recipe, I am replacing the desired word and another string and displaying the updated content on the screen. If you want the updated content to be written into another file, then you can always open another file in write mode and execute the fputs function to write the updated content in it.

Let's use GCC to compile the replaceword.c program, as follows:

D:\CBook>gcc replaceword.c -o replaceword

If you get no errors or warnings, then this means that the replaceword.c program  has been compiled into an executable file, replaceword.exe. Let's run the executable file, replaceword.exe, and supply a text file to it. We will assume that a text file called textfile.txt exists and has the following content:

I am trying to create a sequential file. it is through C programming. It is very hot today. I have a cat. do you like animals? It might rain. Thank you. bye

Now, let's use this file to replace one of its words with another word using the following code:

D:\CBook>./replaceword textfile.txt
Enter a string to be replaced: is
Enter the new string was
I am trying to create a sequential file. it was through C programming. It was very hot today. I have a cat. do you like animals? It might rain. Thank you. Bye

You can see that all occurrences of the word is are replaced by was in textfile.txt, and the modified content is displayed on the screen. We've successfully replaced the words of our choice.

Now, let's move on to the next recipe!