Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function to replace multiple occurence of substring in a string

Reply
Thread Tools

Function to replace multiple occurence of substring in a string

 
 
Dominique Deleris
Guest
Posts: n/a
 
      02-21-2005
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
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      02-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Dominique Deleris) writes:

> 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?
--
Ben Pfaff
email: (E-Mail Removed)
web: http://benpfaff.org
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      02-21-2005
Dominique Deleris <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      02-22-2005
In article <(E-Mail Removed) >,
(E-Mail Removed) says...
> 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.

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      02-27-2005
Dominique Deleris wrote:
> 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.


---
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I replace a substring in a string silverburgh.meryl@gmail.com C++ 7 05-31-2007 02:50 AM
A simple 'Replace' and 'Substring' question: How to add a string to another string SM Javascript 4 04-27-2007 07:22 AM
how to replace a substring in a string using C? Paul C Programming 19 11-10-2005 08:57 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
Multiple scopes for one occurence in XTM-TopicMaps Armin Wagenknecht XML 0 05-04-2004 11:31 AM



Advertisments