Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

looking for a function string substitution
I'm looking for a function that will substitue one string for another
within a string.
similare to perl
s/fromThis/toThis/g;

char* stringSub(char* base,char* fromThis,char* toThis);

char* retStr = stringSub("dogs cats dogs sheep dogs","dogs","mice");

End results will be
"mice cats mice sheep mice"

It should also work is "mice" is an empty string.
char* retStr = stringSub("dogs cats dogs sheep dogs","dogs","");
End results will be
" cats  sheep "

Report this thread to moderator Post Follow-up to this message
Old Post
bpatton
03-24-08 01:12 PM


Re: looking for a function string substitution
On Mar 24, 5:51 am, bpatton <bpat...@ti.com> wrote:
> I'm looking for a function that will substitue one string for another
> within a string.
> similare to perl
> s/fromThis/toThis/g;

Umm, so why don't you find one or write one? Let us know how it's
going.

> char* stringSub(char* base,char* fromThis,char* toThis);
>
> char* retStr = stringSub("dogs cats dogs sheep dogs","dogs","mice");
>
> End results will be
> "mice cats mice sheep mice"

This is not a good idea, since you passed an immutable string to
'stringSub' and seem to expect the string to be modified.

> It should also work is "mice" is an empty string.
> char* retStr = stringSub("dogs cats dogs sheep dogs","dogs","");
> End results will be
> " cats  sheep "

Is the function supposed to return a new string that contains the
modified contents or is it supposed to modify the string passed in? If
the former, who is supposed to free the memory the new string is
using?

It doesn't sound like you have a well-thought-out question yet.

DS

Report this thread to moderator Post Follow-up to this message
Old Post
David Schwartz
03-25-08 12:25 AM


Re: looking for a function string substitution
On Mar 24, 8:13 am, David Schwartz <dav...@webmaster.com> wrote:
> On Mar 24, 5:51 am, bpatton <bpat...@ti.com> wrote:
> 
>
> Umm, so why don't you find one or write one? Let us know how it's
> going.
> 
> 
> 
>
> This is not a good idea, since you passed an immutable string to
> 'stringSub' and seem to expect the string to be modified.
> 
>
> Is the function supposed to return a new string that contains the
> modified contents or is it supposed to modify the string passed in? If
> the former, who is supposed to free the memory the new string is
> using?
>
> It doesn't sound like you have a well-thought-out question yet.
>
> DS



The original string will be unmodified.
A new string will returned that has been created with the fromThis
being replaced by the toThis.  Even if toThis is empty.

Report this thread to moderator Post Follow-up to this message
Old Post
bpatton
03-25-08 12:25 AM


Re: looking for a function string substitution
On Mar 24, 10:12 am, bpatton <bpat...@ti.com> wrote:
> On Mar 24, 8:13 am, David Schwartz <dav...@webmaster.com> wrote:
>
>
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
> The original string will be unmodified.
> A new string will returned that has been created with the fromThis
> being replaced by the toThis.  Even if toThis is empty.


Got it done and here it is :)
#include <string.h>
#include <stdio.h>
#include <malloc.h>

char* myStringSub(char* base,char* fromThis,char* toThis);

int main(void) {
char* ret  = myStringSub("dogs cats dogs sheep dogs and
pigs","dogs","mice");
char* ret1 = myStringSub("dogs cats dogs sheep dogs and
pigs","dogs","");
printf("\n\n");
printf("in  = 'dogs cats dogs sheep dogs and pigs'\n");
printf("sub = 'dogs' for 'cats'\n");
printf("out = '%s'\n",ret);
printf("\n\n");
printf("in  = 'dogs cats dogs sheep dogs and pigs'\n");
printf("sub = 'dogs' for ''\n");
printf("out = '%s'\n",ret1);
return 0;
}

char* myStringSub(char* base,char* fromThis,char* toThis) {
char              *start        = base; // pointer to location in
base
char              *stop         = NULL; // pointer to beginning of
fromThis
char              *next_start   = NULL; // pointer to end of
fromThis in base
char              *next_stop    = NULL; // pointer to next fromThis
in base
char              *new_s        = NULL; // pointer to return string
char              *next         = NULL; // pointer to next instance
of fromThis;
char              *temp         = NULL; // temporary string;
int               len_fromThis  = strlen(fromThis);
int               len_toThis    = strlen(toThis);
int               len_base      = strlen(base);
int               len_new_s     = 0; // contains only a \0
int               new_len       = 0;
int               temp_len      = 0;
new_s    = (char*)malloc (1);
new_s[0] = '\0';
while (stop = strstr(start,fromThis)) {
next_start = stop + len_fromThis;
next     = strstr(stop + len_fromThis,fromThis);
new_len  = len_new_s;   // space for current new_s
new_len += len_toThis; // space for toThis
if (next != NULL) {
new_len += next - next_start; // space for what is between first
and next fromThis
}
new_len += 1; // for null char
temp = (char*) malloc(new_len);
temp[0] = '\0';
temp_len = 0;

//
// add new_s to temp
//
memcpy(temp+temp_len,new_s,len_new_s);
temp_len += len_new_s;
temp[temp_len] = '\0';

//
// add toThis to temp
//
 memcpy(temp+temp_len,toThis,len_toThis);

temp_len += len_toThis;
temp[temp_len] = '\0';

//
// add from end of fromThis to beginning of next fromThis
//
if (next) {
memcpy(temp+temp_len,next_start,next-next_start);
temp_len += next-next_start;
temp[temp_len] = '\0';
}

//
// release memory of new_s
// set new_s to be temp
//
free(new_s);
new_s     = temp;
len_new_s = temp_len;
start     = next_start;
}
//
// no more occurences of fromThis
// check if nore string left after the last fromThis
//
if (start != (base + len_base)) {
new_len  = len_new_s;   // space for current new_s
new_len += (base + len_base) - start; // what left afer last
fromThis
new_len += 1;

temp = (char*) malloc(new_len);
temp[0] = '\0';
temp_len = 0;

//
// add new_s to temp
//
memcpy(temp+temp_len,new_s,len_new_s);
temp_len += len_new_s;
temp[temp_len] = '\0';

//
// now add the remainder of base to to temp
//
memcpy(temp+temp_len,start,(base + len_base) - start);
temp_len += (base + len_base) - start;
temp[temp_len] = '\0';
//
// release memory of new_s
// set new_s to be temp
//
free(new_s);
new_s     = temp;

}
return new_s;
}

Report this thread to moderator Post Follow-up to this message
Old Post
bpatton
03-25-08 12:25 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 02:02 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.