-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove_X.cpp
75 lines (59 loc) · 2.25 KB
/
Remove_X.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<string>
using namespace std;
string removeX(string s)
{
if(s.empty()) //base case
return s;
char first_char = s[0]; //decomposition factor
string rest=s.substr(1); //it takes argument after the first index till the end of string by default bcz of single argument
if(first_char== 'x') //composition factor
return removeX(rest); //process the rest of the string to remove all occurrences of 'x' without the first character
else
return first_char+ removeX(rest); //append the first character with the rest of remaining string if x is not first character
}
int main()
{
string str;
cout<< "enter the string: ";
getline(cin,str);
cout << "String after removing x: " << removeX(str) << endl;
return 0;
}
////remove x from string
// string removeX(string s)
// {
//
//
// if(s.empty()) //base case
// return s;
//
//
// char first_char = s[0]; //decomposition factor
// string rest=s.substr(1); //it takes argument after the first index till the end of string by default bcz of single argument
//
//
// if(first_char== 'x') //composition factor
// return removeX(rest); //process the rest of the string to remove all occurrences of 'x' without the first character
// else
// return first_char+ removeX(rest); //append the first character with the rest of remaining string if x is not first character
// }
//
// //remove y from string
// string removey(string s1)
// {
//
//
// if(s1.empty()) //base case
// return s1;
//
//
// char first_char = s1[0]; //decomposition factor
// string rest=s1.substr(1); //it takes argument after the first index till the end of string by default bcz of single argument
//
//
// if(first_char== 'y') //composition factor
// return removey(rest); //process the rest of the string to remove all occurrences of 'x' without the first character
// else
// return first_char+ removey(rest); //append the first character with the rest of remaining string if x is not first character
// }