#include <stdio.h>
#include <string.h>
char *str_replace(char *s, char *part, char *replace) {
static char tmp[1024];
static char new_s[1024];
char *p;
strcpy(tmp, s);
if (!(p = strstr(tmp, part)))
return tmp;
strncpy(new_s, tmp, p - tmp);
new_s[p-tmp] = '\0';
sprintf(new_s + (p - tmp), "%s%s", replace, p + strlen(part));
sprintf(s, "%s", new_s);
return s;
}
int main(int argc, char **argv)
{
char s[16] = "index.php";
str_replace(s, "php", "c");
puts(s);
return 0;
}
/*
run:
index.c
*/