replacing a string in a char pointer

T

Tim Quon

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
 
T

Tom Zych

Tim said:
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.
 
J

Jirka Klaue

Tim said:
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
 
T

Tim Quon

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);
 
D

Dan Pop

In said:
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
 
D

Dan Pop

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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top