Function to replace multiple occurence of substring in a string

Discussion in 'C Programming' started by Dominique Deleris, Feb 21, 2005.

  1. Hello,

    I desperately and urgently need a __working__ function, that will
    replace all occurences of a substring in a string.

    Prototype should be :
    char *str_replace(char *str, const char *sub_str1, const char
    *sub_str2)

    It should take care of multiple occurences of the substring...

    Please help, as my deadline is here and all functions tried so far do
    not work (strrepl in Snippets, etc...)

    Thanks,

    Dominique
     
    Dominique Deleris, Feb 21, 2005
    #1
    1. Advertising

  2. Dominique Deleris

    Ben Pfaff Guest

    (Dominique Deleris) writes:

    > I desperately and urgently need a __working__ function, that will
    > replace all occurences of a substring in a string.


    Why don't you write a function to do that?
    --
    Ben Pfaff
    email:
    web: http://benpfaff.org
     
    Ben Pfaff, Feb 21, 2005
    #2
    1. Advertising

  3. Dominique Deleris

    -berlin.de Guest

    Dominique Deleris <> wrote:
    > I desperately and urgently need a __working__ function, that will
    > replace all occurences of a substring in a string.


    > Prototype should be :
    > char *str_replace(char *str, const char *sub_str1, const char
    > *sub_str2)


    > It should take care of multiple occurences of the substring...


    Then you should specify the problem more carefully. What happens
    when the substring you are supposed to replace is e.g. "aba" and
    the string is "ababababa"? If the replacememnt is "x" do you want
    "xbxba" or do you expect "xxxx" as the result (or maybe "abxbx"
    if you start at the end)?

    > Please help, as my deadline is here and all functions tried so far do
    > not work (strrepl in Snippets, etc...)


    For the simplest case (i.e. you want "xbxba" in the above example)
    here's some pseudo-code

    char *pos;
    while ( ( pos = strstr( sub_str1, str ) ) != NULL )
    str = do_replacement( pos, strlen( sub_str1 ), str, sub_str2 );

    The do_replacement() function is left as a exercise for the reader.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Feb 21, 2005
    #3
  4. Dominique Deleris

    Randy Howard Guest

    In article <>,
    says...
    > Hello,
    >
    > I desperately and urgently need a __working__ function, that will
    > replace all occurences of a substring in a string.


    What code do you have so far, and how does it fail when you try
    to use it? We don't do homework assignments here, or provide
    free contract services.

    > Prototype should be :
    > char *str_replace(char *str, const char *sub_str1, const char
    > *sub_str2)


    What about buffer overflow? What happens if the string to
    replace is "xyz" and string to replace that with is
    "supercalifragilisticexpialidocious" ?? I smell a segfault
    on the horizon, unless you handle that carefully.

    > It should take care of multiple occurences of the substring...


    As Jens mentioned, there also a lot of degenerate cases that
    might be involved, and you need concrete answers on they
    are to be handled.

    > Please help, as my deadline is here and all functions tried so far do
    > not work (strrepl in Snippets, etc...)


    What have you written, and how does it fail? "do not work" is
    insufficient to help. Deadlines are your problem. Next time,
    start earlier, stay up later, or hire help sooner.

    --
    Randy Howard (2reply remove FOOBAR)
    "Making it hard to do stupid things often makes it hard
    to do smart ones too." -- Andrew Koenig
     
    Randy Howard, Feb 22, 2005
    #4
  5. Dominique Deleris

    Guest

    Dominique Deleris wrote:
    > I desperately and urgently need a __working__ function, that will
    > replace all occurences of a substring in a string.


    Well ... then you should probably get a programmer to do it. :)

    > Prototype should be :
    > char *str_replace(char *str, const char *sub_str1, const char
    > *sub_str2)


    Does it need to be aliasing safe? And what does it return?

    Anyhow "The Better String Library" contains a way to do with with
    "bstrings" (which are kind of like super-char * strings.) The key
    function is bfindreplace(). You can find the source for my library
    here:

    http://bstring.sf.net/

    You can rip out the algorithm and remap it to char * strings. Of
    course, you still have to follow the BSD license if you use the code.
    :)

    ---
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
     
    , Feb 27, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Armin Wagenknecht
    Replies:
    0
    Views:
    390
    Armin Wagenknecht
    May 4, 2004
  2. Paul
    Replies:
    19
    Views:
    13,030
    Netocrat
    Nov 10, 2005
  3. Replies:
    7
    Views:
    454
    Robbie Hatley
    May 31, 2007
  4. Replies:
    3
    Views:
    212
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    216
Loading...

Share This Page