Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > replacing a string in a char pointer

Reply
Thread Tools

replacing a string in a char pointer

 
 
Tim Quon
Guest
Posts: n/a
 
      09-10-2003
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
 
Reply With Quote
 
 
 
 
Tom Zych
Guest
Posts: n/a
 
      09-10-2003
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 '(E-Mail Removed)' | rot13
 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      09-10-2003
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

 
Reply With Quote
 
Tim Quon
Guest
Posts: n/a
 
      09-10-2003
On Wed, 10 Sep 2003 15:35:55 GMT, Tom Zych <(E-Mail Removed)> 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);
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-10-2003
In <(E-Mail Removed)> Tim Quon <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-10-2003
In <(E-Mail Removed)> Tim Quon <(E-Mail Removed)> writes:

>On Wed, 10 Sep 2003 15:35:55 GMT, Tom Zych <(E-Mail Removed)> 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: (E-Mail Removed)
 
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
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments