0% found this document useful (0 votes)
53 views4 pages

Replace A Specific Word With The Given Word in The File

The document outlines a C program designed to find and replace all occurrences of a specified word in a file. It details the algorithm and provides the complete code, which includes reading a source file, replacing the word, and writing the results to a temporary file. The program prompts the user for the file path and the words to replace, ensuring successful execution with appropriate file handling and error checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views4 pages

Replace A Specific Word With The Given Word in The File

The document outlines a C program designed to find and replace all occurrences of a specified word in a file. It details the algorithm and provides the complete code, which includes reading a source file, replacing the word, and writing the results to a temporary file. The program prompts the user for the file path and the words to replace, ensuring successful execution with appropriate file handling and error checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EX 11a.

replace a specific word with the given word in the file

AIM: To Write a C program to find and replace all occurrences of a word in file.

Algorithm:

1. Open source file in r (read) mode, store its reference to fptr.


2. Create a temporary file fTemp in w (write) mode. Store its reference to fTemp.
3. Input old word and new word to replace, from user. Store both
in oldWord and newWord respectively.
4. Read a line from source file, store line read in buffer.
5. Find next occurrence of oldWord in buffer, store its reference in some variable say pos.
6. Copy string buffer to a temporary variable say temp.
7. Find index of first occurrence of oldWord in buffer using pointer arithmetic index = pos -
buffer.
8. Mark index at which first occurrence of oldWord is found as string termination
with NULL character. Use buffer[index] = '\0';.
9. Concatenate string buffer with new word to replace with, say strcat(buffer, newWord);.
10. Concatenate string buffer with remaining words after old word to replace,
say strcat(buffer, temp + index + owlen);. Where owlen is length of oldWord string.
11. Repeat steps 5-10 till occurrence of oldWord is found in buffer. Otherwise goto step 12.
12. Write string buffer to file fTemp.
13. Repeat step 3 till end of source file.
14. Close all files to save changes.

Program:

/* C program to find and replace all occurrences of a word in file. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1000


int main()
{
/* File pointer to hold reference of input file */
FILE * fPtr;
FILE * fTemp;
char path[100];
char *pos, temp[BUFFER_SIZE];
int index = 0;

char buffer[BUFFER_SIZE];
char oldWord[100], newWord[100];
int owlen;

printf("Enter path of source file: ");


scanf("%s", path);

printf("Enter word to replace: ");


scanf("%s", oldWord);

printf("Replace %s with: ",oldWord);


scanf("%s", newWord);

/* Open all required files */


fPtr = fopen(path, "r");
fTemp = fopen("replace.txt", "w");

/* fopen() return NULL if unable to open file in given mode. */


if (fPtr == NULL || fTemp == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read/write privilege.\n");
exit(EXIT_SUCCESS);
}

/*
* Read line from source file and write to destination
* file after replacing given word. */
while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL)
{
// Replace all occurrence of word from current line
owlen = strlen(oldWord);

// Fix: If oldWord and newWord are same it goes to infinite loop


if (! strcmp(oldWord, newWord)) {
break;
}
while ((pos = strstr(buffer, oldWord)) != NULL)
{
// Backup current line
strcpy(temp, buffer);

// Index of current found word


index = pos - buffer;

// Terminate str after word found index


buffer[index] = '\0';

// Concatenate str with new word


strcat(buffer, newWord);

// Concatenate str with remaining words after


// oldword found index.
strcat(buffer, temp + index + owlen);
}

// After replacing write it to temp file.


fputs(buffer, fTemp);
}

/* Close all files to release resource */


fclose(fPtr);
fclose(fTemp);

printf("\nSuccessfully replaced all occurrences of '%s' with '%s'.", oldWord, newWord);


fclose(fPtr);
fclose(fTemp);

return 0;
}

INPUT: file.txt
I am at SCE.
C is a high level programming language.
Programming with files is fun.
Learning C programming at lab is easy and fun.

OUTPUT:
Enter path of source file: E:\C_programming\arrays\strings\file.txt
Enter word to replace: is
Replace is with: ez

Successfully replaced all occurrences of 'is' with 'ez'.

After the replacement of is with ez


Replace.txt
I am at SCE.
C ez a high level programming language.
Programming with files ez fun.
Learning C programming at lab ez easy and fun.

You might also like