473,386 Members | 1,827 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,386 software developers and data experts.

replacing a string in a char pointer

Hi

I have a pointer to char and need to replace a string inside with
another string. Something like that:

char* str_replace(const char* oldString, const char* toBeReplaced,
const char* replaceWith);

How can I code the function if the string is of dynamic length?

Thanks
Tim
Nov 13 '05 #1
5 9086
Tim Quon wrote:
I have a pointer to char and need to replace a string inside with
another string. Something like that:

char* str_replace(const char* oldString, const char* toBeReplaced,
const char* replaceWith);

How can I code the function if the string is of dynamic length?


Your prototype looks like you don't want to touch any of the inputs,
just create a new string and return it. If so, "dynamic length" is
not a relevant concept. With this prototype you'd have to allocate
space and have the caller free it when done.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #2
Tim Quon wrote:
I have a pointer to char and need to replace a string inside with
another string. Something like that:

char* str_replace(const char* oldString, const char* toBeReplaced,
const char* replaceWith);


char *a = "abcdefghijkl", *b = "fg", *c = "xxx", *new = 0, *p;
size_t i = strlen(a), j = strlen(b), k = strlen(c);

if (p = strstr(a, b)) {
new = malloc(i - j + k + 1);
memcpy(new, a, p - a);
memcpy(new + (p - a), c, k + 1);
strcat(new + (p - a) + k, p + j);
}
return new;

Jirka

Nov 13 '05 #3
On Wed, 10 Sep 2003 15:35:55 GMT, Tom Zych <tz******@pobox.com> wrote:
Tim Quon wrote:
I have a pointer to char and need to replace a string inside with
another string. Something like that:

char* str_replace(const char* oldString, const char* toBeReplaced,
const char* replaceWith);

How can I code the function if the string is of dynamic length?


Your prototype looks like you don't want to touch any of the inputs,
just create a new string and return it. If so, "dynamic length" is
not a relevant concept. With this prototype you'd have to allocate
space and have the caller free it when done.


Your're right, better will be:
char *str_replace(char *theString, const char *toBeReplaced, const
char *replaceWith);
Nov 13 '05 #4
In <kr********************************@4ax.com> Tim Quon <ti***@freesurf.ch> writes:
I have a pointer to char and need to replace a string inside with
another string. Something like that:

char* str_replace(const char* oldString, const char* toBeReplaced,
const char* replaceWith);

How can I code the function if the string is of dynamic length?


It doesn't matter: you know the lengths of all the strings involved,
therefore you can compute the length of the result string and dynamically
allocate it. The problem is slightly more difficult if multiple instances
of toBeReplaced have to be replaced by replaceWith, because you have to
(correctly) evaluate the number of substitutions before computing the
length of the output string.

strstr, memcpy and strcat are your friends when coding the function.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
In <ov********************************@4ax.com> Tim Quon <ti***@freesurf.ch> writes:
On Wed, 10 Sep 2003 15:35:55 GMT, Tom Zych <tz******@pobox.com> wrote:
Tim Quon wrote:
I have a pointer to char and need to replace a string inside with
another string. Something like that:

char* str_replace(const char* oldString, const char* toBeReplaced,
const char* replaceWith);

How can I code the function if the string is of dynamic length?


Your prototype looks like you don't want to touch any of the inputs,
just create a new string and return it. If so, "dynamic length" is
not a relevant concept. With this prototype you'd have to allocate
space and have the caller free it when done.


Your're right, better will be:
char *str_replace(char *theString, const char *toBeReplaced, const
char *replaceWith);


Would it? If strlen(replaceWith) > strlen(toBeReplaced) how do you
know whether the operation can be safely performed?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

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

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
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...
4
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...
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!!!!!
10
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; ...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.