473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Function to replace multiple occurence of substring in a string

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
Nov 14 '05 #1
4 7962
dd******@mitsa.ch (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: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #2
Dominique Deleris <dd******@mitsa.ch> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
In article <7c**************************@posting.google.com >,
dd******@mitsa.ch 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
Nov 14 '05 #4
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/

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Nige | last post by:
To save me re-inventing the wheel, does anyone have a function to capitalise each word in form data? I was thinking along the lines of capitalise the first letter of the string, then any other...
7
by: Richard Trahan | last post by:
I need a javascript function to hex-encode a plus sign so I can pass the plus sign as an argument in a GET request. escape() and encodeURI() don't do it (and probably shouldn't, because '+' is a...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
9
by: Kishor | last post by:
Hi all, I am Using VB.Net for developing my application. I am now needed help. In this project I have to execute some function, but I cannot call them directly using function name, I wanted to...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
11
by: youngster94 | last post by:
Hey all, I've written a VB.Net app that creates picture badges complete with barcodes. The problem is that the barcode quality is not good enough to be read by scanners. I'm using the...
7
by: f pemberton | last post by:
I have a string (xdata) and theres a newline after every 17 characters of the string. I was wondering how I can replace multiple substrings multiple times within a string? To put it another way,...
1
by: roots_of_culture | last post by:
Hi all, VB novice bear with me... trying to build myself a template in Outlook to make life easier I am aquiring multiple subtrings from the user and want to replce one substring in a outlook...
1
by: MLH | last post by:
Access 97 doesn't have Replace$ function - at least I couldn't find it. Can someone tell me what the command does so I can adapt Doug's code for my own use in A97? Private Function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.