#include <stdio.h>
#include <string.h>
char *str_replace(char *s, char *part, char *replace, int start) {
static char tmp[1024];
static char new_s[1024];
char *p;
strcpy(tmp, s + start);
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 + start, "%s", new_s);
return s;
}
int main(int argc, char **argv)
{
char s1[32] = "php.index.php";
char s2[32] = "php.index.php";
str_replace(s1, "php", "c", 0);
puts(s1);
str_replace(s2, "php", "c", 5);
puts(s2);
return 0;
}
/*
run:
c.index.php
php.index.c
*/