#C12710. Replace Character in a String
Replace Character in a String
Replace Character in a String
You are given a string s and two characters: old_char and new_char. Your task is to replace every occurrence of old_char in s with new_char. The replacement should be case-sensitive, meaning that only the characters that exactly match old_char (including their case) should be replaced.
For example, if s is "hello world", old_char is 'l' and new_char is 'x', then the output should be "hexxo worxd". If new_char is an empty string, then all occurrences of old_char should be removed.
Note: If old_char does not occur in s, simply print the original string.
You can represent the character replacement function mathematically as follows:
$$ f(s, c, d) = \text{replace}(s, c, d) $$
where \( \text{replace}(s, c, d) \) returns a new string in which every occurrence of character \( c \) in \( s \) is replaced by character \( d \).
inputFormat
The input is read from stdin and consists of three lines:
- The first line contains the string s.
- The second line contains the character old_char to be replaced.
- The third line contains the character new_char which will replace old_char. This line could be empty indicating that old_char should be removed.
outputFormat
Output to stdout a single line containing the modified string after all replacements.
## samplehello world
l
x
hexxo worxd