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!!!!!
Nov 14 '05
13 2373
The following is an untested quick hack. There's something like this
in FreeDOS edlin 2.5 (available on ibiblio or alt.sources):
#include <string.h>
char *replace_patter n(char *s, char *p, char *np)
{
char *x;
size_t plen, nplen;
if (s == NULL || p == NULL || np == NULL)
return NULL;
x = strstr(s, p);
if (x == NULL)
return NULL;
plen = strlen(p);
nplen = strlen(np);
if (plen != nplen)
memmove(x + nplen, x + plen, strlen(x + plen) + 1);
memmove(x + plen, np, nplen);
return s;
}
/* Gregory Pietsch */
"Gregory Pietsch" <GK**@flash.net > writes: The following is an untested quick hack. There's something like this in FreeDOS edlin 2.5 (available on ibiblio or alt.sources):
#include <string.h>
char *replace_patter n(char *s, char *p, char *np) { char *x; size_t plen, nplen;
if (s == NULL || p == NULL || np == NULL) return NULL; x = strstr(s, p); if (x == NULL) return NULL; plen = strlen(p); nplen = strlen(np); if (plen != nplen) memmove(x + nplen, x + plen, strlen(x + plen) + 1); memmove(x + plen, np, nplen); return s; }
You didn't quote the original question, which was:
] How can I replace an occurence of p(a string) in an other string(s) with
] np(new string)..
If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
I haven't studied the code carefully, but one thing that jumps out is
that it returns NULL if p does not occur in s.
It's not clear from the orginal problem statement whether the
replacement is supposed to be done in place (writing over the previous
contents of s) or in a newly allocated string. Given that the
function returns a char* result, I assume the latter; otherwise it
might as well be a void function. In this case, surely replacing all
occurences of "foo" by "bar" in "hello" should return "hello", not
NULL.
Also, your function overwrites the original contents of s; as I
mentioned, it's not clear whether this violates the requirements.
Finally, you don't allocate any memory. That's ok if you want to
overwrite s *and* if np is no longer than p. Otherwise, you need to
allocate some extra space, and if you don't want to overwrite s you
need to allocate space for the entire result (and document that the
caller is responsible for deallocating it).
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote: "Gregory Pietsch" <GK**@flash.net > writes: The following is an untested quick hack. There's something like this in FreeDOS edlin 2.5 (available on ibiblio or alt.sources):
#include <string.h>
char *replace_patter n(char *s, char *p, char *np) { char *x; size_t plen, nplen;
if (s == NULL || p == NULL || np == NULL) return NULL; x = strstr(s, p); if (x == NULL) return NULL; plen = strlen(p); nplen = strlen(np); if (plen != nplen) memmove(x + nplen, x + plen, strlen(x + plen) + 1); memmove(x + plen, np, nplen); return s; } You didn't quote the original question, which was:
] How can I replace an occurence of p(a string) in an other string(s) with ] np(new string)..
If you want to post a followup via groups.google.c om, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers.
I haven't studied the code carefully, but one thing that jumps out is that it returns NULL if p does not occur in s.
It's not clear from the orginal problem statement whether the replacement is supposed to be done in place (writing over the previous contents of s) or in a newly allocated string. Given that the function returns a char* result, I assume the latter; otherwise it might as well be a void function. In this case, surely replacing all occurences of "foo" by "bar" in "hello" should return "hello", not NULL.
Also, your function overwrites the original contents of s; as I mentioned, it's not clear whether this violates the requirements.
Finally, you don't allocate any memory. That's ok if you want to overwrite s *and* if np is no longer than p. Otherwise, you need to allocate some extra space, and if you don't want to overwrite s you need to allocate space for the entire result (and document that the caller is responsible for deallocating it).
As I said, it was a quick hack. Here's one that has a lot of fixes
suggested above.
#include <string.h>
#include <stdlib.h>
char *replace_patter n(char *s, char *p, char *np)
{
char *x, *r;
size_t slen, plen, nplen;
if (s == NULL || p == NULL || np == NULL)
return NULL;
x = strstr(s, p);
if (x == NULL)
return s;
plen = strlen(p);
nplen = strlen(np);
slen = strlen(s);
r = malloc(slen - plen + nplen + 1);
if (r == 0)
abort(); /* Not worried about error conditions */
strcpy(r, s);
if (plen != nplen)
memmove((r + (x - s)) + nplen, x + plen, strlen(x + plen) + 1);
memmove((r + (x - s)) + plen, np, nplen);
return r;
} -- Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Derek Hart |
last post by:
I wish to execute code from a string. The string will have a function name,
which will return a string:
Dim a as string
a = "MyFunctionName(param1, param2)"
I have seen a ton of people discuss how reflection does this, but I cannot
find the syntax to do this. I have tried several code example off of
gotdotnet and other articles. Can somebody please show me the code to do
this?
|
by: Danny |
last post by:
HI again
Is there a nifty function in access that will:
1. return the amount of occurances of a small string within a larger
string?
this<br>is<br>a<br>test
would return 3 for <br>
|
by: songkv |
last post by:
Hi,
I am trying to reassign an array of char to a string literal by
calling a function.
In the function I use pointer-to-pointer since I want to reassign the
"string array pointer" to the string literal.
But the second printf seems to give me garbage.
Any advise on what I am doing wrong?
Thanx
|
by: Simon Schaap |
last post by:
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where a * occurs in
the string). This split function should allocate a 2D array of chars
and put the split results in different rows. The listing below shows
how I started to work on this. To keep the program simple and help
focus the program the...
|
by: intercom5 |
last post by:
I'm writing a program in C, and thus have to use C strings. The
problem that I am having is I don't know how to reallocate the space
for a C string outside the scope of that string. For example:
int main(void)
{
char *string1;
string1 = malloc(6);
sprintf(string1, "Hello");
| |
by: Cor |
last post by:
Hallo,
I have promised Jay B yesterday to do some tests.
The subject was a string evaluation that Jon had send in. Jay B was in doubt
what was better because there was a discussion in the C# newsgroup on 25
September. The regular expressions where in that newsgroup too involved.
I told yesterday night, to Jay that I would test all 4 methods and the
stupid method I was thinking of the first time that night when I saw Jon's
|
by: Sia Jai Sung |
last post by:
Hi,
I have a class that I modify from a sample program, like below
==========================================
Imports System
Imports System.Web.UI
Imports System.Security.Cryptography
public Class CSoccerichCommonFunc
|
by: rsine |
last post by:
I have developed a program that sends a command through the serial port
to our business system and then reads from the buffer looking for a
number. Everything worked great on my WinXP system, but when I tried
the program on the Win98 system it will be running on, I get the
following error:
Cast from string "2076719" to type 'Long' is not valid
I am not sure why I only get this error on the Win98 system or how to go
about correcting...
|
by: Tom |
last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For
instance, I want to do something like the following:
If strTest Contains ("A","B", "C") Then
Debug.WriteLine("Found characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If
|
by: Darren Anderson |
last post by:
I have a function that I've tried using in an if then statement and I've
found that no matter how much reworking I do with the code, the expected
result is incorrect.
the code:
If Not (strIn.Substring(410, 10).Trim = "") Then
'Something processed
Else
'Something processed
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |