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 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';
}