-
Notifications
You must be signed in to change notification settings - Fork 1
/
EndX.cpp
41 lines (30 loc) · 850 Bytes
/
EndX.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
/*
Given a string, compute recursively a new string where all the lowercase 'x' chars have been moved to the end of the string.
endX("xxre") → "rexx"
endX("xxhixx") → "hixxxx"
endX("xhixhix") → "hihixxx"
*/
#include <iostream>
using namespace std;
string endX(string str, int index){
string result;
if(index == str.length()-1 || str.length() == 1){
result += str[index];
return result;
}
if(index > str.length())
return result;
if(str[index] != 'x'){
result += str[index];
return (result + endX(str, index + 1));
}
else{
return (endX(str, index + 1) + "x");
}
}
int main(){
cout << "Enter String : ";
string str;
cin >> str;
cout << "New string where lowercase 'x' chars in " << str << " are moved to end is " << endX(str, 0) << endl;
}