« Remove x recursively from a string

Problem

Given a string, compute recursively a new string where all 'x' chars have been removed.

Input format :

String S

Output format :

Modified String

Constraints :

1 <= |S| <= 10^3

where |S| represents the length of string S.

Sample Input 1 :

xaxb

Sample Output 1:

ab

Sample Input 2 :

abc

Sample Output 2:

abc

Sample Input 3 :

xx

Sample Output 3:

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 removeX(char input[], int start)
15{
16 if (input[start] == '\0')
17 {
18 return;
19 }
20 removeX(input, start + 1);
21 if (input[start] == 'x')
22 {
23 int i = start + 1;
24 while (input[i] != '\0')
25 {
26 input[i - 1] = input[i];
27 i++;
28 }
29 input[i - 1] = '\0';
30 }
31}
32
33void removeX(char input[])
34{
35 // Write your code here
36 removeX(input, 0);
37}
38
39int main()
40{
41 char input[100];
42 cin.getline(input, 100);
43 removeX(input);
44 cout << input << endl;
45 return 0;
46}