cgen.c
char *replace_key_with_value(const char *str, const char *key, const char *value)
{
	size_t key_len = strlen(key);
	if (key_len == 0) return strdup(str);

	int count_of_key = 0;
	const char *occurence = str;
	while ((occurence = strstr(occurence, key)) != NULL) {
		count_of_key++;
		occurence += key_len;
	}

	size_t value_len = strlen(value);
	size_t result_len = strlen(str) + count_of_key * (value_len - key_len);
	char *result = malloc(result_len + 1);
	
	const char *src = str;
	char *dst = result;
	while ((occurence = strstr(src, key)) != NULL) {
		strncpy(dst, src, occurence - src);
		dst += occurence - src;
		strcpy(dst, value);
		dst += value_len;
		src = occurence + key_len;		
	}
	strcpy(dst, src);

	return result;
}
/* replace_key_with_value returns an allocated string. The caller owns the return string. */
replace_key_with_value returns an allocated string. The caller owns the return string.
replace_key_with_value returns an allocated string . The caller owns the return string .
char *replace_key_with_value(const char *str, const char *key, const char *value)
{
	size_t key_len = strlen(key);
	if (key_len == 0) return strdup(str);
	int count_of_key = 0;
	const char *occurence = str;
	while ((occurence = strstr(occurence, key)) != NULL) {
		count_of_key++;
		occurence += key_len;
	}
	size_t value_len = strlen(value);
	size_t result_len = strlen(str) + count_of_key * (value_len - key_len);
	char *result = malloc(result_len + 1);
	const char *src = str;
	char *dst = result;
	while ((occurence = strstr(src, key)) != NULL) {
		strncpy(dst, src, occurence - src);
		dst += occurence - src;
		strcpy(dst, value);
		dst += value_len;
		src = occurence + key_len;		
	}
	strcpy(dst, src);
	return result;
}
char * replace_key_with_value ( const char * str , const char * key , const char * value ) { size_t key_len = strlen ( key ) ; if ( key_len == 0 ) return strdup ( str ) ; int count_of_key = 0 ; const char * occurence = str ; while ( ( occurence = strstr ( occurence , key ) ) != NULL ) { count_of_key ++ ; occurence += key_len ; } size_t value_len = strlen ( value ) ; size_t result_len = strlen ( str ) + count_of_key * ( value_len - key_len ) ; char * result = malloc ( result_len + 1 ) ; const char * src = str ; char * dst = result ; while ( ( occurence = strstr ( src , key ) ) != NULL ) { strncpy ( dst , src , occurence - src ) ; dst += occurence - src ; strcpy ( dst , value ) ; dst += value_len ; src = occurence + key_len ; } strcpy ( dst , src ) ; return result ; }
replace_key_with_value returns an allocated string.
replace_key_with_value returns an allocated string .