# Recursive program to replace all occurrences of pi with 3.14 in a given string

Given string str of size N. The task is to write a recursive function to replace all occurrences of pi with 3.14 in the given string and print the modified string.

Examples:

Input : str = “pippppiiiipi”
Output : 3.14ppp3.14iii3.14

Input : str = “pip”
Output : 3.14p

Input : str = “xpix”
Output : x3.14x

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We have discussed an iterative function here

Approach :

• If there is only one character in a string or the string is empty break the recursive call
• Else keep the first character of the string with yourself and pass the rest of the string to recursion.
• If the first character is not ‘p’ then just put that character in front of the answer which came from recursion
• Else if the first character is ‘p’ and the first character of the part passed to recursion is ‘i’ then replace “pi” with “3.14”

Below is the implementation of the above approach:

## C++

 `// A recursive C++ program to replace ` `// all pi in a given string with 3.14 ` `#include ` `using` `namespace` `std; ` ` `  `// Recursive Function to replace all ` `// occurrences of pi in a given ` `// with 3.14 ` `void` `replacePiHelper(``char` `str[], ``int` `start) ` `{ ` ` `  `    ``// Base condition ` `    ``// if the string is empty ` `    ``// or of length one ` `    ``if` `(str[start] == ``'\0'` `|| str[start] == ``'\0'``) { ` `        ``return``; ` `    ``} ` ` `  `    ``// Getting the answer from ` `    ``// recursion for the smaller ` `    ``// problem ` `    ``replacePiHelper(str, start + 1); ` ` `  `    ``// Small calculation part ` `    ``// if the first character is 'p' ` `    ``// and the first character of the part ` `    ``// passed to recursion is 'i' then replace ` `    ``// "pi" with "3.14" ` `    ``if` `(str[start] == ``'p'` `&& str[start + 1] == ``'i'``) { ` ` `  `        ``// Shifting the characeters to ` `        ``// right side to put 3.14 in ` `        ``// the character array ` `        ``for` `(``int` `i = ``strlen``(str); i >= start + 2; i--) { ` `            ``str[i + 2] = str[i]; ` `        ``} ` ` `  `        ``// Replacing with "3.14" ` `        ``str[start] = ``'3'``; ` `        ``str[start + 1] = ``'.'``; ` `        ``str[start + 2] = ``'1'``; ` `        ``str[start + 3] = ``'4'``; ` `    ``} ` `} ` ` `  `// Function to replace pi with 3.14 ` `void` `replacePi(``char` `str[]) ` `{ ` `    ``replacePiHelper(str, 0); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``char` `str[] = ``"pippppiiiipi"``; ` ` `  `    ``// Function call ` `    ``replacePi(str); ` ` `  `    ``cout << str; ` ` `  `    ``return` `0; ` `} `

## Java

 `// A recursive Java program to replace ` `// all pi in a given string with 3.14 ` ` `  `class` `GFG { ` ` `  `    ``// Recursive Function to replace all ` `    ``// occurrences of pi in a given ` `    ``// with 3.14 ` `    ``public` `String replacePi(String str) ` `    ``{ ` `        ``// base condition ` `        ``// if the string is empty ` `        ``// or of length one ` `        ``if` `(str.length() <= ``1``) { ` `            ``return` `str; ` `        ``} ` ` `  `        ``// if the first character is 'p' ` `        ``// and the first character of the part ` `        ``// passed to recursion is 'i' then replace ` `        ``//"pi" with "3.14" ` `        ``if` `(str.charAt(``0``) == ``'p'` `&& str.length() >= ``2` `            ``&& str.charAt(``1``) == ``'i'``) { ` `            ``return` `"3.14"` `+ replacePi(str.substring(``2``, str.length())); ` `        ``} ` ` `  `        ``// if the first character is not 'p' ` `        ``// then just put that character in ` `        ``// front of the answer which came ` `        ``// from recursion ` `        ``return` `str.charAt(``0``) + replacePi(str.substring(``1``, str.length())); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``GFG g = ``new` `GFG(); ` `        ``String str = ``"pippppiiiipi"``; ` `        ``System.out.println(g.replacePi(str)); ` `    ``} ` `} `

## Python3

 `# A recursive Python3 program to replace ` `# all pi in a given string with 3.14 ` ` `  `# Recursive Function to replace all ` `# occurrences of pi in a given ` `# with 3.14 ` `def` `replacePieHelper(string, start): ` ` `  `    ``# Base condition ` `    ``# if the string is empty ` `    ``# or of length one ` `    ``if` `len``(string) < ``2` `or` `start ``=``=` `len``(string): ` `        ``return` `string ` ` `  `    ``# Getting the answer from ` `    ``# recursion for the smaller ` `    ``# problem ` `    ``replacePieHelper(string, start ``+` `1``) ` ` `  `    ``# Small calculation part ` `    ``# if the first character is 'p' ` `    ``# and the first character of the part ` `    ``# passed to recursion is 'i' then replace ` `    ``# "pi" with "3.14" ` `    ``if``(string[start] ``=``=` `'p'` `and`  `       ``string[start ``+` `1``] ``=``=` `'i'``): ` ` `  `        ``# Replacing with "3.14" ` `        ``string[start:start ``+` `2``] ``=` `[``'3'``, ``'.'``, ``'1'``, ``'4'``] ` ` `  `# Function to replace pi with 3.14 ` `def` `replacePi(string): ` `    ``replacePieHelper(string, ``0``) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``string ``=` `"pippppiiiipi"` ` `  `    ``string ``=` `list``(string) ` ` `  `    ``# Function call ` `    ``replacePi(string) ` ` `  `    ``string ``=` `''.join(string) ` `    ``print``(string) ` ` `  `# This code is contributed by ` `# sanjeev2552 `

## C#

 `// A recursive C# program to replace ` `// all pi in a given string with 3.14 ` ` `  `using` `System; ` `class` `gfg { ` `    ``// Recursive Function to replace all ` `    ``// occurrences of pi in a given ` `    ``// with 3.14 ` `    ``public` `String replacePi(String str) ` `    ``{ ` `        ``// base condition ` `        ``// if the string is empty ` `        ``// or of length one ` `        ``if` `(str.Length <= 1) { ` `            ``return` `str; ` `        ``} ` ` `  `        ``// if the first character is 'p' ` `        ``// and the first character of the part ` `        ``// passed to recursion is 'i' then replace ` `        ``//"pi" with "3.14" ` `        ``if` `(str == ``'p'` `&& str.Length >= 2 ` `            ``&& str == ``'i'``) { ` `            ``return` `"3.14"` `+ replacePi(str.Substring(2, str.Length - 2)); ` `        ``} ` ` `  `        ``// if the first character is not 'p' ` `        ``// then just put that character in ` `        ``// front of the answer which came ` `        ``// from recursion ` `        ``return` `str + replacePi(str.Substring(1, str.Length - 1)); ` `    ``} ` `} ` ` `  `// Driver Code ` `class` `geek { ` `    ``public` `static` `int` `Main() ` `    ``{ ` `        ``gfg g = ``new` `gfg(); ` `        ``string` `input = ``"pippppiiiipi"``; ` `        ``Console.WriteLine(g.replacePi(input)); ` `        ``return` `0; ` `    ``} ` `} `

Output:

```3.14ppp3.14iii3.14
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : sanjeev2552

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6894729,"math_prob":0.83061,"size":6375,"snap":"2020-10-2020-16","text_gpt3_token_len":1808,"char_repetition_ratio":0.17909276,"word_repetition_ratio":0.25577417,"special_character_ratio":0.3145098,"punctuation_ratio":0.10711225,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.995733,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-04-05T11:00:06Z\",\"WARC-Record-ID\":\"<urn:uuid:a379c0f6-3c11-4f66-a331-5d387b432b4f>\",\"Content-Length\":\"160529\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:71f8b485-eaa5-499f-86c3-ef477b9d54ee>\",\"WARC-Concurrent-To\":\"<urn:uuid:18e5cbe0-6bbf-47d4-8705-a341f1a8f2e3>\",\"WARC-IP-Address\":\"23.221.227.168\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/recursive-program-to-replace-all-occurrences-of-pi-with-3-14-in-a-given-string/\",\"WARC-Payload-Digest\":\"sha1:XIRVTO7M3YWIVNF5TN63WYVQKNUKMU4B\",\"WARC-Block-Digest\":\"sha1:CIYDGPEOXGPP62GFUALT3YSPJJQ22IAK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585371576284.74_warc_CC-MAIN-20200405084121-20200405114121-00196.warc.gz\"}"}