Different ways

Discussion in 'C Programming' started by tfelb, Jan 16, 2009.

  1. tfelb

    tfelb Guest

    Hi group!

    I'm coding a str_replace function and I'm trying to figure out which
    possibilities in C exists to increase the size
    of the (destination string)

    You all know how a string_replace function works.

    char text[] = "This is a test sentence";

    If i want to replace the word 'test' with a word of the same size then
    I have no problem but what happens if the
    replacement is larger then the whole array or the whole word!? e.x.
    test' with 'teeeeeeeeeeeeeest'!?
    So there must be ways to increase the size of the destination string.

    I think I have the following possibilities:

    1.)

    char *str_replace(char *src, char *find, char *replacement);
    {

    /* Pseudo code: */

    char *dst = malloc/calloc + size of src array + size of replacement
    array

    and return dst


    }

    main:

    char newString[32];
    newString = str_replace(text,'test','teeeeeeeeeeeeeest');

    2.)

    The other way I think is

    void str_replace(char *dst, char *src, char *find, char *replacement);
    {

    /* Pseudocode: */

    char *p = dst;
    and so on..

    }

    main:

    char newString[32];
    str_replace(newString, text, 'test', 'teeeeeeeeeeeeest');


    3.)

    void str_replace(char *dst, char *src, char *find, char *replacement)
    {
    char **p;
    /* Assigning each word to p. At the end I'll assign each element
    of p to dst */



    }

    main:
    + the same as 2.)


    Are the other ways to realize a string_replace function?


    Thank you all for any ideas!

    Tom
     
    tfelb, Jan 16, 2009
    #1
    1. Advertising

  2. tfelb <> writes:

    > I'm coding a str_replace function and I'm trying to figure out which
    > possibilities in C exists to increase the size
    > of the (destination string)
    >
    > You all know how a string_replace function works.
    >
    > char text[] = "This is a test sentence";
    >
    > If i want to replace the word 'test' with a word of the same size then
    > I have no problem but what happens if the
    > replacement is larger then the whole array or the whole word!? e.x.
    > test' with 'teeeeeeeeeeeeeest'!?
    > So there must be ways to increase the size of the destination
    > string.


    It depends on what you mean...

    > I think I have the following possibilities:
    >
    > 1.)
    >
    > char *str_replace(char *src, char *find, char *replacement);
    > {
    >
    > /* Pseudo code: */
    >
    > char *dst = malloc/calloc + size of src array + size of replacement
    > array
    >
    > and return dst


    OK. You could get away with a smaller string, but this works.

    > }
    >
    > main:
    >
    > char newString[32];
    > newString = str_replace(text,'test','teeeeeeeeeeeeeest');


    But this call does not. Arrays are not assignable in C.

    > 2.)
    >
    > The other way I think is
    >
    > void str_replace(char *dst, char *src, char *find, char *replacement);
    > {
    >
    > /* Pseudocode: */
    >
    > char *p = dst;
    > and so on..
    >
    > }
    >
    > main:
    >
    > char newString[32];
    > str_replace(newString, text, 'test', 'teeeeeeeeeeeeest');


    Right, but this dose not "increase the size" -- the space is simply
    there, provided by the caller. If you do this, it helps to pass in
    the size so the function can avoid writing beyond the end.

    > 3.)
    >
    > void str_replace(char *dst, char *src, char *find, char *replacement)
    > {
    > char **p;
    > /* Assigning each word to p. At the end I'll assign each element
    > of p to dst */


    I don't understand this so I can't say.

    > }
    >
    > main:
    > + the same as 2.)
    >
    > Are the other ways to realize a string_replace function?


    Yes, but they all depend on other ways to get the strings in and out.
    The basic problem of having enough room for the new string can only be
    solved in two ways: either the caller passes in the array to use for
    the result, or the function allocates it itself.

    There is an inconvenient small variation on this second option. You
    insist that the string is in allocated memory and you use realloc to
    grow it. This is inconvenient because you can't pass static or
    automatic arrays to such a function.

    --
    Ben.
     
    Ben Bacarisse, Jan 16, 2009
    #2
    1. Advertising

  3. tfelb

    tfelb Guest

    On 16 Jan., 16:36, Ben Bacarisse <> wrote:
    > tfelb <> writes:
    > > I'm coding a str_replace function and I'm trying to figure out which
    > > possibilities in C exists to increase the size
    > > of the (destination string)

    >
    > > You all know how a string_replace function works.

    >
    > > char text[] = "This is a test sentence";

    >
    > > If i want to replace the word 'test' with a word of the same size then
    > > I have no problem but what happens if the
    > > replacement is larger then the whole array or the whole word!? e.x.
    > > test' with 'teeeeeeeeeeeeeest'!?
    > > So there must be ways to increase the size of the destination
    > > string.

    >
    > It depends on what you mean...
    >
    > > I think I have the following possibilities:

    >
    > > 1.)

    >
    > > char *str_replace(char *src, char *find, char *replacement);
    > > {

    >
    > >  /* Pseudo code: */

    >
    > >  char *dst = malloc/calloc + size of src array + size of replacement
    > > array

    >
    > >  and return dst

    >
    > OK.  You could get away with a smaller string, but this works.
    >
    > > }

    >
    > > main:

    >
    > >  char newString[32];
    > >  newString = str_replace(text,'test','teeeeeeeeeeeeeest');

    >
    > But this call does not.  Arrays are not assignable in C.
    >
    >
    >
    >
    >
    > > 2.)

    >
    > > The other way I think is

    >
    > > void str_replace(char *dst, char *src, char *find, char *replacement);
    > > {

    >
    > >    /* Pseudocode: */

    >
    > >    char *p = dst;
    > >    and so on..

    >
    > > }

    >
    > > main:

    >
    > >   char newString[32];
    > >   str_replace(newString, text, 'test', 'teeeeeeeeeeeeest');

    >
    > Right, but this dose not "increase the size" -- the space is simply
    > there, provided by the caller.  If you do this, it helps to pass in
    > the size so the function can avoid writing beyond the end.
    >
    > > 3.)

    >
    > > void str_replace(char *dst, char *src, char *find, char *replacement)
    > > {
    > >       char **p;
    > >       /* Assigning each word to p. At the end I'll assign each element
    > > of p to dst */

    >
    > I don't understand this so I can't say.
    >
    > > }

    >
    > >   main:
    > >   + the same as 2.)

    >
    > > Are the other ways to realize a string_replace function?

    >
    > Yes, but they all depend on other ways to get the strings in and out.
    > The basic problem of having enough room for the new string can only be
    > solved in two ways: either the caller passes in the array to use for
    > the result, or the function allocates it itself.
    >
    > There is an inconvenient small variation on this second option.  You
    > insist that the string is in allocated memory and you use realloc to
    > grow it.  This is inconvenient because you can't pass static or
    > automatic arrays to such a function.
    >
    > --
    > Ben.- Zitierten Text ausblenden -
    >
    > - Zitierten Text anzeigen -


    >two ways: either the caller passes in the array to use for
    > the result, or the function allocates it itself.



    hmm you mean something like that (quick & dirty) where I pass the
    array (dst) to the function

    void str_replace(char *dst, char *src, char *find, char *replace)
    {
    char *p = strstr(src,find);
    char *buffer = dst;
    size_t offset = p - src;
    int rest = offset + strlen(replace) - 1;

    memcpy(buffer,src,offset);
    src += rest;
    buffer += offset;

    memmove(buffer,replace,strlen(replace));
    memmove(buffer+strlen(replace),src,strlen(src));
    buffer[strlen(replace)+strlen(src)] = '\0';

    }
     
    tfelb, Jan 16, 2009
    #3
  4. tfelb <> writes:

    <snip my whole reply>

    Please snip those parts you are not going to comment on leaving enough
    context so the whole post still makes sense.

    >>two ways: either the caller passes in the array to use for
    >> the result, or the function allocates it itself.

    >
    >
    > hmm you mean something like that (quick & dirty) where I pass the
    > array (dst) to the function


    Yes, but I also suggested (that was too weak, I *strongly* suggest)
    that you pass the bffset size in as well so the function can avoid
    over-filling it.

    > void str_replace(char *dst, char *src, char *find, char *replace)
    > {
    > char *p = strstr(src,find);
    > char *buffer = dst;
    > size_t offset = p - src;
    > int rest = offset + strlen(replace) - 1;
    >
    > memcpy(buffer,src,offset);
    > src += rest;
    > buffer += offset;
    >
    > memmove(buffer,replace,strlen(replace));
    > memmove(buffer+strlen(replace),src,strlen(src));
    > buffer[strlen(replace)+strlen(src)] = '\0';
    >
    > }


    Not what I'd write, but once you correct the bugs it would fit the
    specification. How do you decide when t use memcpy and when to use
    memmove? I don't see any logic to your choice.

    --
    Ben.
     
    Ben Bacarisse, Jan 16, 2009
    #4
  5. tfelb

    tfelb Guest

    On 16 Jan., 18:35, Ben Bacarisse <> wrote:
    > tfelb <> writes:
    >
    > <snip my whole reply>
    >
    > Please snip those parts you are not going to comment on leaving enough
    > context so the whole post still makes sense.
    >
    > >>two ways: either the caller passes in the array to use for
    > >> the result, or the function allocates it itself.

    >
    > > hmm you mean something like that (quick & dirty) where I pass the
    > > array (dst) to the function

    >
    > Yes, but I also suggested (that was too weak, I *strongly* suggest)
    > that you pass the bffset size in as well so the function can avoid
    > over-filling it.
    >
    >
    >
    >
    >
    > > void str_replace(char *dst, char *src, char *find, char *replace)
    > > {
    > >           char *p = strstr(src,find);
    > >     char *buffer = dst;
    > >     size_t offset = p - src;
    > >     int rest = offset + strlen(replace) - 1;

    >
    > >           memcpy(buffer,src,offset);
    > >           src += rest;
    > >     buffer += offset;

    >
    > >     memmove(buffer,replace,strlen(replace));
    > >     memmove(buffer+strlen(replace),src,strlen(src));
    > >     buffer[strlen(replace)+strlen(src)] = '\0';

    >
    > > }

    >
    > Not what I'd write, but once you correct the bugs it would fit the
    > specification.  How do you decide when t use memcpy and when to use
    > memmove?  I don't see any logic to your choice.
    >
    > --
    > Ben.- Zitierten Text ausblenden -
    >
    > - Zitierten Text anzeigen -


    I'm not an c expert like you but I'm happy with your suggestions.
    Thanks!. I know this function has no security checks but it was only
    for fun.

    My knowledge in C is not deep enough thats why I use memcpy first
    ( copying to dst) and then memmove. Maybe I must look into Harbison/
    Steele C Reference manual for clarity.
     
    tfelb, Jan 16, 2009
    #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. Øyvind Isaksen
    Replies:
    3
    Views:
    519
    Patrick.O.Ige
    Dec 7, 2005
  2. Rodney Edmondson
    Replies:
    11
    Views:
    684
    Jeremy
    Dec 9, 2003
  3. Todd MacCulloch

    different ways to creating two lists from one

    Todd MacCulloch, Oct 29, 2003, in forum: Python
    Replies:
    3
    Views:
    301
    Fredrik Lundh
    Oct 29, 2003
  4. rtilley

    different ways to strip strings

    rtilley, Feb 27, 2006, in forum: Python
    Replies:
    6
    Views:
    565
    Steven D'Aprano
    Feb 27, 2006
  5. JS

    Different ways to cast?

    JS, Mar 24, 2005, in forum: C Programming
    Replies:
    6
    Views:
    330
    Richard Bos
    Mar 25, 2005
Loading...

Share This Page