write a c program to replace 1 character with another character in a given file

24 Dec 2022 Balmiki Mandal 0 C Programming

C Program to Replace Characters in a File

"Write a C program to replace 1 character with another character in a given file" means that you need to write a program in the C programming language that takes a file as input and replaces a specific character in the file with another character. The program should be able to locate the specific character in the file and replace it with the desired character, and then save the modified file back to its original location.

For example, if the input file contains the character 'a' and you want to replace it with the character 'b', the program should locate all occurrences of 'a' in the file and replace them with 'b'. Once the replacement is done, the program should save the modified file back to its original location, overwriting the original contents of the file.

Overall, the task involves working with file input/output functions in the C programming language to read, modify, and write data to a file.

Example 01: C program that replaces a specified character with another character in a given file

#include<stdio.h>
void main(int argc,char **argv)
{
    if(argc!=4)
    {
        printf("usage:./a.out fname char\n");
        return;
    }
    FILE *fp;
    fp=fopen(argv[1],"r+");
    if(fp=0)
    {
        printf("file not present..\n");
        return;
    }
    char ch;
    int c=0;
    /////////////////////////////
    while((ch=fgetc(fp))!=-1)
    c++;
    rewind(fp);
    ///////////////////////////
    char *p;
    p=malloc(c+1);
    ////////////////////////////
    int i=0;
    while((ch=fgetc(fp))!=-1)
    p[i++]=ch;
    p[i]='\0';
    /////////////////////
    for(i=0;p[i];i++)
    {
        if(p[i]==argv[2][0])
        p[i]=argv[3][0];
    }
    ////////////////////////
    // for(i=0;p[i];i++)
    //fputc(p[i],fp);
    fputs(p,fp);
}

To compile and run the program, you can use the following commands:

gcc -o replace_character replace_character.c
./replace_character myfile.txt a b

This will compile the program and create an executable file called replace_character. You can then run the program by typing ./replace_character myfile.txt a b. This will replace all occurrences of the character a in the file myfile.txt with the character b.

 

The output of the program will be the modified file myfile.txt.

Example 02: C program that replaces a specified character with another character in a given file

#include<stdio.h>

int main() {
    FILE *fp;
    char filename[100], find_char, replace_char;

    printf("Enter the filename: ");
    scanf("%s", filename);

    printf("Enter the character to find: ");
    scanf(" %c", &find_char);

    printf("Enter the character to replace with: ");
    scanf(" %c", &replace_char);

    // Open file for reading and writing
    fp = fopen(filename, "r+");

    if (fp == NULL) {
        printf("Could not open file %s", filename);
        return 1;
    }

    // Read file character by character
    char c;
    while ((c = fgetc(fp)) != EOF) {
        // Replace character if found
        if (c == find_char) {
            fseek(fp, -1, SEEK_CUR);
            fputc(replace_char, fp);
        }
    }

    fclose(fp);

    printf("Successfully replaced '%c' with '%c' in file '%s'", find_char, replace_char, filename);

    return 0;
}

program reads the file character by character. For each character read, the program checks if it matches the character to find. If it does, the program repositions the file pointer to the current position and writes the character to replace with to the file.

Once the program has reached the end of the file, it closes the file.

The output of the program is a message indicating that the character was successfully replaced in the file.

For example, if the user enters the filename myfile.txt, the character to find a, and the character to replace with b, then the program will print the following output:

Successfully replaced 'a' with 'b' in file 'myfile.txt'

 

The file myfile.txt will also be modified, with all occurrences of the character a replaced with the character b.

Here's how it works:

  • The user is prompted to enter the filename, character to find and character to replace with.
  • The program opens the file for reading and writing using the "r+" mode.
  • The program reads the file character by character using fgetc().
  • If the character to find is found, the program replaces it with the character to replace with using fseek() and fputc().
  • Finally, the program closes the file and displays a success message.

Note that this program only replaces the first occurrence of the character to find. If you want to replace all occurrences, you can modify the program to use a loop to search for and replace all occurrences.

Top Resources

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.