« Replace character recursively

Problem

Given an input string S and two characters c1 and c2, you need to replace every occurrence of character c1 with character c2 in the given string. Do this recursively.

Input Format :

Line 1 : Input String S

Line 2 : Character c1 and c2 (separated by space)

Output Format :

Updated string

Constraints :

1 <= Length of String S <= 10^6

Sample Input :

abacd

a x

Sample Output :

xbxcd

Solution

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14void replaceCharacter(char input[], char c1, char c2)
15{
16 if (input[0] == '\0')
17 {
18 return;
19 }
20 if (input[0] == c1)
21 {
22 input[0] = c2;
23 }
24 replaceCharacter(input + 1, c1, c2);
25}
26int main()
27{
28 char input[1000000];
29 char c1, c2;
30 cin >> input;
31 cin >> c1 >> c2;
32 replaceCharacter(input, c1, c2);
33 cout << input << endl;
34 return 0;
35}