Skip to content

luc097.c

Problem Statement

Write a program using command-line arguments to search for a word in a file and replace it with the specified word.\nUsage: change

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Beginner (index: 3 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Pointers
  • Recursion
  • Array
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

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

void replace_all(char *str, const char *old_w, const char *new_w, FILE *ft);

int main(int argc, char *argv[])
{
    FILE *fp, *ft;
    char line[1000];
    char *old_word, *new_word, *filename;

    if (argc != 4)
    {
        printf("Usage: %s <old word> <new word> <filename>\n", argv[0]);
        exit(1);
    }

    old_word = argv[1];
    new_word = argv[2];
    filename = argv[3];

    fp = fopen(filename, "r");
    if (fp == NULL)
    {
        printf("Error opening file: %s\n", filename);
        exit(2);
    }

    // Create a temporary file
    ft = fopen("temp.tmp", "w");
    if (ft == NULL)
    {
        printf("Error creating temporary file.\n");
        fclose(fp);
        exit(3);
    }

    // Process line by line
    while (fgets(line, sizeof(line), fp))
    {
        replace_all(line, old_word, new_word, ft);
    }

    fclose(fp);
    fclose(ft);

    // Replace original file with updated file
    remove(filename);
    rename("temp.tmp", filename);

    printf("Replacement complete.\n");

    return 0;
}

void replace_all(char *str, const char *old_w, const char *new_w, FILE *ft)
{
    char *pos, temp[1000];
    int index = 0;
    int old_len = strlen(old_w);

    /* We cannot easily modify 'str' in place because new_w 
       might be larger than old_w. We write directly to file.
    */

    while ((pos = strstr(str, old_w)) != NULL)
    {
        // Write everything before the match
        while (str < pos)
        {
            fputc(*str, ft);
            str++;
        }

        // Write the new word
        fputs(new_w, ft);

        // Skip the old word in the source string
        str += old_len;
    }

    // Write the remainder of the line
    fputs(str, ft);
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc097.c):

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

    void replace_all(char *str, const char *old_w, const char *new_w, FILE *ft);

    int main(int argc, char *argv[])
    {
        FILE *fp, *ft;
        char line[1000];
        char *old_word, *new_word, *filename;

        if (argc != 4)
        {
            printf("Usage: %s <old word> <new word> <filename>\n", argv[0]);
            exit(1);
        }

        old_word = argv[1];
        new_word = argv[2];
        filename = argv[3];

        fp = fopen(filename, "r");
        if (fp == NULL)
        {
            printf("Error opening file: %s\n", filename);
            exit(2);
        }

        // Create a temporary file
        ft = fopen("temp.tmp", "w");
        if (ft == NULL)
        {
            printf("Error creating temporary file.\n");
            fclose(fp);
            exit(3);
        }

        // Process line by line
        while (fgets(line, sizeof(line), fp))
        {
            replace_all(line, old_word, new_word, ft);
        }

        fclose(fp);
        fclose(ft);

        // Replace original file with updated file
        remove(filename);
        rename("temp.tmp", filename);

        printf("Replacement complete.\n");

        return 0;
    }

    void replace_all(char *str, const char *old_w, const char *new_w, FILE *ft)
    {
        char *pos, temp[1000];
        int index = 0;
        int old_len = strlen(old_w);

        /* We cannot easily modify 'str' in place because new_w 
           might be larger than old_w. We write directly to file.
        */

        while ((pos = strstr(str, old_w)) != NULL)
        {
            // Write everything before the match
            while (str < pos)
            {
                fputc(*str, ft);
                str++;
            }

            // Write the new word
            fputs(new_w, ft);

            // Skip the old word in the source string
            str += old_len;
        }

        // Write the remainder of the lin
    ... (truncated for brevity)