Most of the modules I found online will replace just the first occurrence of that string. But I want a module that will search every occurrence and will replace that string. I found a code in stack overflow which was so very tedious, so I'm not looking into it. And the environment where I'm working, supports only C compiler. So C++ command tweaks comments are not appreciated. This is as far as I've come. It still throws me errors. Any help is appreciated.
#include <stdio.h>
#include <string.h>
char *replace_str(char *str, char *orig, char *rep)
{
char *buffer;
char *p, *work;
while(strstr(str,orig))
{
p = strstr(str, orig);
strncpy(work, str, p-str);
strcat(buffer, work);
//strncpy(buffer+strlen(buffer), str, p-str);
strcat(buffer, rep);
p+=strlen(orig);
str = p;
}
return strcat(buffer,p);
}
int main(void)
{
puts(replace_str("Hello, Kate! I once had a cat named Kate! ", "Kate", "Paul"));
return 0;
}