Some questions

T

tfelb

Hi Group!

I wrote a function to replace a string with another string. It works
fine but only

1. if the number of word1(the finding word) and word2(the replacement)
is equal.
2. It only replaces the first word

Any suggestions to improve this function? For example, to replace all
words or a longer word?
No, it's not a homework question ;-).

Thank you for all!


Tom

char string[] = "Yeah, C Programming is wonderful Yeah";
replace(string,"Yeah","test");
Output: test, C Programming is wonderful Yeah


void replace(char *dst, char *find ,char *replace)
{
char *p = dst;
char *pos = strstr(dst,find);

while(*p++)
while(*find)
{
find++;
if(*pos)
{
*pos++ = *replace++;
}
}
*p = '\0';
}
 
C

CBFalconer

tfelb said:
I wrote a function to replace a string with another string. It
works fine but only

1. if the number of word1(the finding word) and word2(the
replacement) is equal.
2. It only replaces the first word

Any suggestions to improve this function? For example, to
replace all words or a longer word?
No, it's not a homework question ;-).

You may want to look at the technique of selecting id strings used
in id2id_20, which in turn is available (under GPL) at:

<http://cbfalconer.home.att.net/download/>

Note that the techniques selects complete ids, not substrings. I
have also published methods using kmp techniques in this
newsgroup. I believe the program was name binfsrch. These methods
can pick out strings.
 
G

gw7rib

Hi Group!

I wrote a function to replace a string with another string. It works
fine but only

1. if the number of word1(the finding word) and word2(the replacement)
is equal.
2. It only replaces the first word

Any suggestions to improve this function? For example, to replace all
words or a longer word?
No, it's not a homework question ;-).

Thank you for all!

Tom

char string[] = "Yeah, C Programming is wonderful Yeah";
replace(string,"Yeah","test");
Output: test, C Programming is wonderful Yeah

void replace(char *dst, char *find ,char *replace)
{
char *p = dst;
char *pos = strstr(dst,find);

while(*p++)
while(*find)
{
find++;
  if(*pos)
  {
   *pos++ = *replace++;
  }



}
*p = '\0';
}

I presume you're writing this function to learn, so I won't tell you
the complete answer, but give you a nudge in the right direction.

To answer your second point first - you only appear to actually call
strstr once, so you are only going to find one occurence of "Yeah".
You need to get this test into the loop. Another problem is that you
are increasing the values of "find" and "replace", so that eventually
they will both point to the zeros at the end of the original strings,
but if you want to do a second search and replace you need to find
trhe beginnings to the strings again. Try storing the values in one
variable and incrementing another one.

On your first point - if you are going to replace one string with
another the same length, then the characters which do not get replaced
will stay as they are. However, if you are replacing a string with a
different length one, the characters which aren't replaced will need
to be moved forwards or backwards appropriately. This is a bit more
tricky - though well within your capabilities, I'm sure. One thing to
watch out for if the replacement string is longer is to make sure that
you do not start writing characters beyond the space you are allowed
to.

If you start getting problems, there's no substitute for getting out a
bit of paper, drawing some boxes on it to represent where the
charcters are stored, write in a few test characters and draw a few
arrows to show where your pointers are pointing to. Read through your
program and move the arrows appropriately. It'll give you a much
better idea what's going on.

Best of luck!
Paul.
 
J

jameschen

Hi Group!
I wrote a function to replace a string with another string. It works
fine but only
1. if the number of word1(the finding word) and word2(the replacement)
is equal.
2. It only replaces the first word
Any suggestions to improve this function? For example, to replace all
words or a longer word?
No, it's not a homework question ;-).
Thank you for all!

char string[] = "Yeah, C Programming is wonderful Yeah";
replace(string,"Yeah","test");
Output: test, C Programming is wonderful Yeah
void replace(char *dst, char *find ,char *replace)
{
char *p = dst;
char *pos = strstr(dst,find);
while(*p++)
while(*find)
{
find++;
  if(*pos)
  {
   *pos++ = *replace++;
  }
}
*p = '\0';
}

I presume you're writing this function to learn, so I won't tell you
the complete answer, but give you a nudge in the right direction.

To answer your second point first - you only appear to actually call
strstr once, so you are only going to find one occurence of "Yeah".
You need to get this test into the loop. Another problem is that you
are increasing the values of "find" and "replace", so that eventually
they will both point to the zeros at the end of the original strings,
but if you want to do a second search and replace you need to find
trhe beginnings to the strings again. Try storing the values in one
variable and incrementing another one.

On your first point - if you are going to replace one string with
another the same length, then the characters which do not get replaced
will stay as they are. However, if you are replacing a string with a
different length one, the characters which aren't replaced will need
to be moved forwards or backwards appropriately. This is a bit more
tricky - though well within your capabilities, I'm sure. One thing to
watch out for if the replacement string is longer is to make sure that
you do not start writing characters beyond the space you are allowed
to.

If you start getting problems, there's no substitute for getting out a
bit of paper, drawing some boxes on it to represent where the
charcters are stored, write in a few test characters and draw a few
arrows to show where your pointers are pointing to. Read through your
program and move the arrows appropriately. It'll give you a much
better idea what's going on.

Best of luck!
Paul.- Hide quoted text -

- Show quoted text -

I would like to give some hints on your second point.
1) use strstr to check word1 appear times
2) caculate destination string length, and malloc the size
3) copy result to destination string
4) done
 
T

tfelb

Hi Group!
I wrote a function to replace a string with another string. It works
fine but only
1. if the number of word1(the finding word) and word2(the replacement)
is equal.
2. It only replaces the first word
Any suggestions to improve this function? For example, to replace all
words or a longer word?
No, it's not a homework question ;-).
Thank you for all!
Tom
char string[] = "Yeah, C Programming is wonderful Yeah";
replace(string,"Yeah","test");
Output: test, C Programming is wonderful Yeah
void replace(char *dst, char *find ,char *replace)
{
char *p = dst;
char *pos = strstr(dst,find);
while(*p++)
while(*find)
{
find++;
  if(*pos)
  {
   *pos++ = *replace++;
  }
}
*p = '\0';
}
I presume you're writing this function to learn, so I won't tell you
the complete answer, but give you a nudge in the right direction.
To answer your second point first - you only appear to actually call
strstr once, so you are only going to find one occurence of "Yeah".
You need to get this test into the loop. Another problem is that you
are increasing the values of "find" and "replace", so that eventually
they will both point to the zeros at the end of the original strings,
but if you want to do a second search and replace you need to find
trhe beginnings to the strings again. Try storing the values in one
variable and incrementing another one.
On your first point - if you are going to replace one string with
another the same length, then the characters which do not get replaced
will stay as they are. However, if you are replacing a string with a
different length one, the characters which aren't replaced will need
to be moved forwards or backwards appropriately. This is a bit more
tricky - though well within your capabilities, I'm sure. One thing to
watch out for if the replacement string is longer is to make sure that
you do not start writing characters beyond the space you are allowed
to.
If you start getting problems, there's no substitute for getting out a
bit of paper, drawing some boxes on it to represent where the
charcters are stored, write in a few test characters and draw a few
arrows to show where your pointers are pointing to. Read through your
program and move the arrows appropriately. It'll give you a much
better idea what's going on.
Best of luck!
Paul.- Hide quoted text -
- Show quoted text -

I would like to give some hints on your second point.
1) use strstr to check word1 appear times
2) caculate destination string length, and malloc the size
3) copy result to destination string
4) done- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Yes, that's true I write this function to learn. I like string
functions very much.
Thanks for the suggestions! I'll try.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top