For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2008 > looking for a function string substitution









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author looking for a function string substitution
bpatton

2008-03-24, 8:12 am

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 "
David Schwartz

2008-03-24, 7:25 pm

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
bpatton

2008-03-24, 7:25 pm

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.
bpatton

2008-03-24, 7:25 pm

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;
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com