Function to replace multiple occurence of substring in a string

D

Dominique Deleris

Hello,

I desperately and urgently need a __working__ function, that will
replace all occurences of a substring in a string.

Prototype should be :
char *str_replace(char *str, const char *sub_str1, const char
*sub_str2)

It should take care of multiple occurences of the substring...

Please help, as my deadline is here and all functions tried so far do
not work (strrepl in Snippets, etc...)

Thanks,

Dominique
 
B

Ben Pfaff

I desperately and urgently need a __working__ function, that will
replace all occurences of a substring in a string.

Why don't you write a function to do that?
 
J

Jens.Toerring

Dominique Deleris said:
I desperately and urgently need a __working__ function, that will
replace all occurences of a substring in a string.
Prototype should be :
char *str_replace(char *str, const char *sub_str1, const char
*sub_str2)
It should take care of multiple occurences of the substring...

Then you should specify the problem more carefully. What happens
when the substring you are supposed to replace is e.g. "aba" and
the string is "ababababa"? If the replacememnt is "x" do you want
"xbxba" or do you expect "xxxx" as the result (or maybe "abxbx"
if you start at the end)?
Please help, as my deadline is here and all functions tried so far do
not work (strrepl in Snippets, etc...)

For the simplest case (i.e. you want "xbxba" in the above example)
here's some pseudo-code

char *pos;
while ( ( pos = strstr( sub_str1, str ) ) != NULL )
str = do_replacement( pos, strlen( sub_str1 ), str, sub_str2 );

The do_replacement() function is left as a exercise for the reader.

Regards, Jens
 
R

Randy Howard

Hello,

I desperately and urgently need a __working__ function, that will
replace all occurences of a substring in a string.

What code do you have so far, and how does it fail when you try
to use it? We don't do homework assignments here, or provide
free contract services.
Prototype should be :
char *str_replace(char *str, const char *sub_str1, const char
*sub_str2)

What about buffer overflow? What happens if the string to
replace is "xyz" and string to replace that with is
"supercalifragilisticexpialidocious" ?? I smell a segfault
on the horizon, unless you handle that carefully.
It should take care of multiple occurences of the substring...

As Jens mentioned, there also a lot of degenerate cases that
might be involved, and you need concrete answers on they
are to be handled.
Please help, as my deadline is here and all functions tried so far do
not work (strrepl in Snippets, etc...)

What have you written, and how does it fail? "do not work" is
insufficient to help. Deadlines are your problem. Next time,
start earlier, stay up later, or hire help sooner.
 
W

websnarf

Dominique said:
I desperately and urgently need a __working__ function, that will
replace all occurences of a substring in a string.

Well ... then you should probably get a programmer to do it. :)
Prototype should be :
char *str_replace(char *str, const char *sub_str1, const char
*sub_str2)

Does it need to be aliasing safe? And what does it return?

Anyhow "The Better String Library" contains a way to do with with
"bstrings" (which are kind of like super-char * strings.) The key
function is bfindreplace(). You can find the source for my library
here:

http://bstring.sf.net/

You can rip out the algorithm and remap it to char * strings. Of
course, you still have to follow the BSD license if you use the code.
:)
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top