forked from till-tomorrow/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReplacePi.cpp
More file actions
25 lines (22 loc) · 668 Bytes
/
ReplacePi.cpp
File metadata and controls
25 lines (22 loc) · 668 Bytes
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
///Replace all the occurrences of pi in a string
#include<iostream>
#include<string.h>
using namespace std;
char* replaceAllOccurrencesOfPi(char str[], int length, int startIndex){
if(length == 0) return '';
if(startIndex == length) return '';
char temp[] = replaceAllOccurrencesOfPi(str, length, startIndex + 1);
if(str[startIndex] == 'p' && temp[0] == 'i'){
str[startIndex] = 'P';
temp[0] = 'I';
}
return (str[startIndex]).concat(temp);
}
int main(){
char arr[] = {'p','i','p','t','w'};
char ans[] = replaceAllOccurrencesOfPi(arr, 5, 0);
for(int i = 0; i < 5; i++){
cout<<ans[i];
}
return 0;
}