Question About Finding a Word and Replacing It

This is a discussion on Question About Finding a Word and Replacing It within the C Programming forums, part of the General Programming Boards category; Hello, I asked a question about this program a few days ago, but now I have a new problem. What ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Question Question About Finding a Word and Replacing It

    Hello,

    I asked a question about this program a few days ago, but now I have a new problem. What I want to do now is search through the line that's read in from stdin, and then replace one word with another (provided from command line arguments). I'm trying to use the strstr() function to check if the word is there, but I can't think of how to replace the word with the other one. Here is what I have:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main(int argc, char *argv[]) {
        char buf[BUFSIZ];
    
        // read lines from stdin
        while ( fgets(buf, BUFSIZ, stdin) ) {
            if (strstr(buf,argv[1]) == NULL) puts(buf);
            else puts(argv[2]);
            }
    All that does is prints out argv[2] when strstr is not null. Any help would be greatly appreciated.

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >but I can't think of how to replace the word with the other one.
    You can hope like crazy that the string you're looking for is the whole of the buffer, or you can use a replace type function similar to below:
    Code:
    Tue Sep 23 9:37:12am
    sangut: ~/misc
    > cat rp.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *replace(const char *s1, const char *find, const char *repl) {
      char *rs;
      char *loc;
      int  slen = strlen(s1);
      int  flen = strlen(find);
      int  rlen = strlen(repl);
    
      if ((loc = strstr(s1, find)) != NULL) {
        if ((rs = calloc(slen - flen + rlen  + 1, sizeof *rs)) == NULL)
          return NULL;
        strncpy(rs, s1, loc - s1);
        strcat(rs, repl);
        strcat(rs, loc + strlen(find));
    
        return rs;
      }
    
      return NULL;
    }
    
    int main(int argc, char *argv[]) {
      char buffer[BUFSIZ];
      char *fixed;
    
      if (argc != 3) {
        fprintf(stderr, "usage: % rp <find> <replace>\n");
        exit(EXIT_FAILURE);
      }
      while (fgets(buffer, sizeof buffer, stdin) != NULL) {
        if ((fixed = replace(buffer, argv[1], argv[2])) != NULL)
          puts(fixed);
        else
          puts(buffer);
      }
    
      return 0;
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    [nitpicks]

    >puts(fixed);

    It may be better to use fputs(fixed, stdout); to avoid an extra newline in addition to the one that fgets leaves; same for puts(buffer);.

    >fprintf(stderr, "usage: % rp <find> <replace>\n");

    fprintf(stderr, "usage: %% rp <find> <replace>\n");

    >char *loc;
    >if ((loc = strstr(s1, find)) != NULL)

    You could declare loc as a const char * to preserve s1's constness.


    Memory allocated should also be freed.

    I prefer to use size_t variables for the result of strlen. YMMV.

    [/nitpicks]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >It may be better to use fputs(fixed, stdout);
    My excuse is quick and dirty code.

    >fprintf(stderr, "usage: %% rp <find> <replace>\n");
    Hmm, no excuse for that one. Oh well, serves me right for not using more strict compilation options.

    >You could declare loc as a const char * to preserve s1's constness.
    Quick and dirty.

    >Memory allocated should also be freed.
    I don't bother if it's a one time quickie program. The system will handle memory release when my process goes poof.

    >I prefer to use size_t variables for the result of strlen.
    Yes, but size_t is twice as big as int. You can't possibly expect me to type an extra 3 characters just to match types, can you?
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replacing a part of a string with an other???
    By pseudonoma in forum C Programming
    Replies: 10
    Last Post: 03-24-2008, 09:31 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Replies: 7
    Last Post: 06-16-2006, 10:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21