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