replace_string()

replace_string()#

replace_string() replaces all occurrences of a substring within a given string with another substring, dynamically allocating memory for the new string.

Prototype

char *replace_string(
    const char *str,
    const char *old,
    const char *snew
);

Parameters

Key

Type

Description

str

const char *

The input string in which replacements will be made.

old

const char *

The substring to be replaced.

snew

const char *

The substring to replace occurrences of old.


Return Value

A newly allocated string with all occurrences of old replaced by snew. Returns NULL if memory allocation fails.

Notes

The caller is responsible for freeing the returned string using free(). If old is an empty string, the function will enter an infinite loop.

Prototype

// Not applicable in JS

Prototype

# Not applicable in Python
Examples
// TODO C examples
// TODO JS examples
# TODO Python examples