bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc097.c (2360B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 08 Feb 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program using command-line arguments to search for a word in a file and replace it with the specified word.\nUsage: change <old word> <new word> <filename>
      9 */
     10 /* Let Us C, Chap- 20 (More Issues In Input/Output), Qn No.: A(c) */
     11 
     12 /* This file is auto-generated by a bot. */
     13 /* This code is not compiled; it is for reference only. */
     14 
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <ctype.h>
     20 
     21 void replace_all(char *str, const char *old_w, const char *new_w, FILE *ft);
     22 
     23 int main(int argc, char *argv[])
     24 {
     25     FILE *fp, *ft;
     26     char line[1000];
     27     char *old_word, *new_word, *filename;
     28 
     29     if (argc != 4)
     30     {
     31         printf("Usage: %s <old word> <new word> <filename>\n", argv[0]);
     32         exit(1);
     33     }
     34 
     35     old_word = argv[1];
     36     new_word = argv[2];
     37     filename = argv[3];
     38 
     39     fp = fopen(filename, "r");
     40     if (fp == NULL)
     41     {
     42         printf("Error opening file: %s\n", filename);
     43         exit(2);
     44     }
     45 
     46     // Create a temporary file
     47     ft = fopen("temp.tmp", "w");
     48     if (ft == NULL)
     49     {
     50         printf("Error creating temporary file.\n");
     51         fclose(fp);
     52         exit(3);
     53     }
     54 
     55     // Process line by line
     56     while (fgets(line, sizeof(line), fp))
     57     {
     58         replace_all(line, old_word, new_word, ft);
     59     }
     60 
     61     fclose(fp);
     62     fclose(ft);
     63 
     64     // Replace original file with updated file
     65     remove(filename);
     66     rename("temp.tmp", filename);
     67 
     68     printf("Replacement complete.\n");
     69 
     70     return 0;
     71 }
     72 
     73 void replace_all(char *str, const char *old_w, const char *new_w, FILE *ft)
     74 {
     75     char *pos, temp[1000];
     76     int index = 0;
     77     int old_len = strlen(old_w);
     78 
     79     /* We cannot easily modify 'str' in place because new_w 
     80        might be larger than old_w. We write directly to file.
     81     */
     82     
     83     while ((pos = strstr(str, old_w)) != NULL)
     84     {
     85         // Write everything before the match
     86         while (str < pos)
     87         {
     88             fputc(*str, ft);
     89             str++;
     90         }
     91         
     92         // Write the new word
     93         fputs(new_w, ft);
     94         
     95         // Skip the old word in the source string
     96         str += old_len;
     97     }
     98     
     99     // Write the remainder of the line
    100     fputs(str, ft);
    101 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror