Thread: Replacing a substring with another

  1. #1
    Join Date
    Nov 2005

    Replacing a substring with another

    I'm creating this program that is going to:
    1. Read a file
    2. Find occurances of a substring within the string read in
    3. Replace the occurances of this substring with another string
    4. Write to new file
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 512
    #define _GNU_SOURCE
    int main(int argc, char *argv[])
      FILE *file, *file2;
      char line[MAX], *subLocation;
      if (argc != 4)
        printf("You must enter: ./replace old-string new-string file-name\n");
      file = fopen(argv[3], "r");
      file2 = fopen("temp", "w");
      while (fgets(line,sizeof(line),file) != NULL)
        /* Search for string 1 in string buffer */
        if (subLocation == NULL)
          subLocation = strstr(line, argv[1]);
         /* position = subLocation-line */
        /* Write the line */
        fputs(line, file2);
        printf(line); /*Show me the line */
      /* Close everything */
      fclose (file);
      fclose (file2);
      return 0;
    I have 1 and 4 down lol. Now I'm pretty new to C...though I have done C++. It's fairly different and I was hoping you guys can shed some light onto some functions I can use for finding and replacing the substring.
    I've so far got strstr which will find the first occurance of the string (and i can loop to see if there is more than one). But I'm not sure what I should use to replace it. Any help would be greatly appreciated .


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > subLocation = strstr(line, argv[1]);
    Given a string of "LEFTMIDDLERIGHT", and you searched for MIDDLE

    subLocation-line gives you 4 (the length of LEFT)
    subLocation+strlen(argv[1]) gives you a pointer to RIGHT

    - write out the left
    - write out the replacement
    - advance to the RIGHT
    - repeat until there's no more string
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Join Date
    Nov 2005
    Thanks for clearing it up

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    printf(line); /*Show me the line */
    You shouldn't do this, in case the line contains a %. Use
    printf("%s", line);
    or better yet
    which is the same as
    printf("%s\n", line);

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split string and remove substring
    By nyc_680 in forum C Programming
    Replies: 3
    Last Post: 03-02-2009, 04:45 AM
  2. I need help with creating a substring program
    By CProgramingBegg in forum C Programming
    Replies: 9
    Last Post: 02-06-2009, 09:50 AM
  3. Replies: 3
    Last Post: 10-14-2008, 05:56 PM
  4. Replies: 11
    Last Post: 12-11-2005, 11:50 PM
  5. Getting a substring help plz
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-01-2005, 06:11 PM