This question already has an answer here:
- What is the function to replace string in C? 18 answers
I need a function from the standard library that replaces all occurrences of a character in a string by another character.
I also need a function from the standard library that replaces all occurrences of a substring in a string by another string.
Are there any such functions in the standard library?
There is no direct function to do that. You have to write something like this, using
strchr
:For whole strings, I refer to this answered question
There are not such functions in the standard libraries.
You can easily roll your own using
strchr
for replacing one single char, orstrstr
to replace a substring (the latter will be slightly more complex).This one returns the number of chars replaced and is even immune to replacing a char by itself