replacing a string in a char pointer

Discussion in 'C Programming' started by Tim Quon, Sep 10, 2003.

  1. Tim Quon

    Tim Quon Guest

    Hi

    I have a pointer to char and need to replace a string inside with
    another string. Something like that:

    char* str_replace(const char* oldString, const char* toBeReplaced,
    const char* replaceWith);

    How can I code the function if the string is of dynamic length?

    Thanks
    Tim
     
    Tim Quon, Sep 10, 2003
    #1
    1. Advertising

  2. Tim Quon

    Tom Zych Guest

    Tim Quon wrote:

    > I have a pointer to char and need to replace a string inside with
    > another string. Something like that:
    >
    > char* str_replace(const char* oldString, const char* toBeReplaced,
    > const char* replaceWith);
    >
    > How can I code the function if the string is of dynamic length?


    Your prototype looks like you don't want to touch any of the inputs,
    just create a new string and return it. If so, "dynamic length" is
    not a relevant concept. With this prototype you'd have to allocate
    space and have the caller free it when done.

    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo '' | rot13
     
    Tom Zych, Sep 10, 2003
    #2
    1. Advertising

  3. Tim Quon

    Jirka Klaue Guest

    Tim Quon wrote:
    > I have a pointer to char and need to replace a string inside with
    > another string. Something like that:
    >
    > char* str_replace(const char* oldString, const char* toBeReplaced,
    > const char* replaceWith);


    char *a = "abcdefghijkl", *b = "fg", *c = "xxx", *new = 0, *p;
    size_t i = strlen(a), j = strlen(b), k = strlen(c);

    if (p = strstr(a, b)) {
    new = malloc(i - j + k + 1);
    memcpy(new, a, p - a);
    memcpy(new + (p - a), c, k + 1);
    strcat(new + (p - a) + k, p + j);
    }
    return new;

    Jirka
     
    Jirka Klaue, Sep 10, 2003
    #3
  4. Tim Quon

    Tim Quon Guest

    On Wed, 10 Sep 2003 15:35:55 GMT, Tom Zych <> wrote:

    >Tim Quon wrote:
    >
    >> I have a pointer to char and need to replace a string inside with
    >> another string. Something like that:
    >>
    >> char* str_replace(const char* oldString, const char* toBeReplaced,
    >> const char* replaceWith);
    >>
    >> How can I code the function if the string is of dynamic length?

    >
    >Your prototype looks like you don't want to touch any of the inputs,
    >just create a new string and return it. If so, "dynamic length" is
    >not a relevant concept. With this prototype you'd have to allocate
    >space and have the caller free it when done.


    Your're right, better will be:
    char *str_replace(char *theString, const char *toBeReplaced, const
    char *replaceWith);
     
    Tim Quon, Sep 10, 2003
    #4
  5. Tim Quon

    Dan Pop Guest

    In <> Tim Quon <> writes:

    >I have a pointer to char and need to replace a string inside with
    >another string. Something like that:
    >
    >char* str_replace(const char* oldString, const char* toBeReplaced,
    >const char* replaceWith);
    >
    >How can I code the function if the string is of dynamic length?


    It doesn't matter: you know the lengths of all the strings involved,
    therefore you can compute the length of the result string and dynamically
    allocate it. The problem is slightly more difficult if multiple instances
    of toBeReplaced have to be replaced by replaceWith, because you have to
    (correctly) evaluate the number of substitutions before computing the
    length of the output string.

    strstr, memcpy and strcat are your friends when coding the function.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 10, 2003
    #5
  6. Tim Quon

    Dan Pop Guest

    In <> Tim Quon <> writes:

    >On Wed, 10 Sep 2003 15:35:55 GMT, Tom Zych <> wrote:
    >
    >>Tim Quon wrote:
    >>
    >>> I have a pointer to char and need to replace a string inside with
    >>> another string. Something like that:
    >>>
    >>> char* str_replace(const char* oldString, const char* toBeReplaced,
    >>> const char* replaceWith);
    >>>
    >>> How can I code the function if the string is of dynamic length?

    >>
    >>Your prototype looks like you don't want to touch any of the inputs,
    >>just create a new string and return it. If so, "dynamic length" is
    >>not a relevant concept. With this prototype you'd have to allocate
    >>space and have the caller free it when done.

    >
    >Your're right, better will be:
    >char *str_replace(char *theString, const char *toBeReplaced, const
    >char *replaceWith);


    Would it? If strlen(replaceWith) > strlen(toBeReplaced) how do you
    know whether the operation can be safely performed?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 10, 2003
    #6
    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. wwj
    Replies:
    7
    Views:
    597
  2. wwj
    Replies:
    24
    Views:
    2,573
    Mike Wahler
    Nov 7, 2003
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,156
    Ian Collins
    May 9, 2006
  4. Sonnich Jensen

    replacing char no 2 in a string?

    Sonnich Jensen, Oct 12, 2011, in forum: C++
    Replies:
    3
    Views:
    361
    Arne Mertz
    Oct 13, 2011
  5. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    316
    Chris Hohmann
    Apr 11, 2005
Loading...

Share This Page