str_replace的c语言实现

//./str_replace_all "(uid=%u/%u)" "%u" chantra
char *str_replace(const char *string, const char *substr, const char *replacement )
{
	char *tok    = NULL;
	char *newstr = NULL;
	char *oldstr = NULL;
	
	/* if either substr or replacement is NULL, duplicate string a let caller handle it */
	if ( substr == NULL || replacement == NULL ) 
		return strdup (string);
	
	newstr = strdup (string);
	while ( (tok = strstr( newstr, substr)))
	{
		oldstr = newstr;
		newstr = malloc (strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
		/*failed to alloc mem, free old string and return NULL */
		if (newstr == NULL)
		{
			free (oldstr);
			return NULL;
		}
		memcpy ( newstr, oldstr, tok - oldstr );
		memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
		memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
		memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 );
		
		free(oldstr);
	}

	return newstr;
}

你可能感兴趣的:(C++,c,C#)