TITLE:-
Replace Character Recursively
PROBLEM:-
Line 1 : Input String S
Line 2 : Character c1 and c2 (separated by space)
Updated string
abacd
a x
xbxcd
SOLUTION:-
#include <iostream>
using namespace std;
void replaceCharacter(char input[], char c1, char c2) {
/* Don't write main().
* Don't read input, it is passed as function argument.
* No need to print or return the output.
* Change in the given input string itself.
* Taking input and printing output is handled automatically.
*/
if(input[0]=='\0')
return;
if(input[0]!=c1)
replaceCharacter(input+1,c1,c2);
else{
input[0]=c2;
replaceCharacter(input+1,c1,c2);
}
}
int main() {
char input[1000000];
char c1, c2;
cin >> input;
cin >> c1 >> c2;
replaceCharacter(input, c1, c2);
cout << input << endl;
}
This is dummy text. It is not meant to be read. Accordingly, it is difficult to figure out when to end it. But then, this is dummy text. It is not meant to be read. Period.
If you have any doubts then please let me know... ConversionConversion EmoticonEmoticon