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.
Input : str = “pippppiiiipi”
Output : 3.14ppp3.14iii3.14
Input : str = “pip”
Output : 3.14p
Input : str = “xpix”
Output : x3.14x
We have discussed an iterative function here
- 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:
- Replace all occurrences of pi with 3.14 in a given string
- Replace all occurrences of a string with space
- Replace all occurrences of string AB with C without using extra space
- Program to reverse a string (Iterative and Recursive)
- Program to replace every space in a string with hyphen
- Program to count vowels in a string (Iterative and Recursive)
- In-place replace multiple occurrences of a pattern
- String slicing in Python to check if a string can become empty by recursive deletion
- Count occurrences of a string that can be constructed from another given string
- Count of occurrences of a "1(0+)1" pattern in a string
- Remove all occurrences of a character in a string
- Count occurrences of a word in string
- Count occurrences of a character in a repeated string
- Rearrange a binary string as alternate x and y occurrences
- Swap all occurrences of two characters to get lexicographically smallest string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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