Different ways

T

tfelb

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
 
B

Ben Bacarisse

tfelb said:
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.
 
T

tfelb

tfelb said:
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:

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.
}

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

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




The other way I think is
void str_replace(char *dst, char *src, char *find, char *replacement);
{
   /* Pseudocode: */
   char *p = dst;
   and so on..

  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.
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.
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';

}
 
B

Ben Bacarisse

<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.
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.
 
T

tfelb

<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.
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.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top