All pastes #69596 Raw Edit

Anonymous

public c v1 · immutable
#69596 ·published 2006-06-23 04:18 UTC
rendered paste body
// replace one instance of from with to in str strgchar* str_replace(const gchar* str, const gchar* from, const gchar* to) {	gchar* resultant = NULL;	int i, j;	int str_length = strlen(str);	int from_length = strlen(from);	int to_length = strlen(to);		gchar* loc_substr = NULL;	 	if (loc_substr = strstr(str, from)) {		int head_size = loc_substr - str;		int tail_size = str_length - head_size - from_length + 1;				g_assert(str_length + to_length - from_length + 1 > 0);		resultant = (gchar*)malloc(str_length + to_length - from_length + 1);		// copy the beginning of the string		memcpy(resultant, str, head_size);		// copy the replacement		memcpy(head_size + resultant, to, to_length);		// copy the tail (+1 for 0x00)		memcpy(head_size + resultant + to_length, loc_substr + from_length, tail_size);				return resultant;	} else {			return g_strdup(str);	}}