2012年4月11日 星期三

C - replace substring


char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;

if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;

strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';

sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

return buffer;
}

Example:
replace_str("Hello, world!", "world", "Miami@ #")

沒有留言:

張貼留言