Since "girl" is longer than "guy", I don't believe your straight replacement logic will work correctly.
This works for a straight word replacement where the words are equal length, but replace "dog" with "girl" and now the space before "is" will be overwritten.
Code:
#include <string.h>
#include <stdio.h>
int main(void) {
char s[] = "that guy is a great guy";
char *dest = s;
char *from = "guy";
char *to = "dog";
char *c=NULL;
printf("destination prior to memmove: %s\n", dest);
while(c = strstr(dest, from)) {
if(c)
memmove(c, to, 3);
dest++;
}
printf(" source after memmove: %s\n", s);
printf("\n\n\t\t\t press enter when ready");
(void) getchar();
return 0;
}
I think the logic you need is:
Code:
while strstr() finds another instance of *to
take each char before the pointer that strstr() returns, and put them into another
string (a much longer string). I believe strcat() will work ok, even on an empty string,
but check that.
strcat the to, onto this string
increment the pointer, same as above
loop back for another strstr() address, if there is one
What do you think?