Thread: Replacing a substring with another

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    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
    Code:
    #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");
        exit(1);
      }
    
      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 .

    Thanks,
    Dan

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

    then
    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
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks for clearing it up
    Dan

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

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    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