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 4 7962
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
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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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!!!!!
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |