Recursive program to insert a star between pair of identical characters - GeeksforGeeks

27 May, 2021
Given a string with repeated characters, we have to insert a star i.e.” * “ between pair of adjacent identical characters using recursion. 
Examples: 
Input : aabb 
Output : a*ab*b

Input : xxxy
Output : x*x*xy 
Approach: 
If there is an empty string then simply return. This forms our base condition.
Check if the first two characters are identical. If yes, then insert ” * ” between them.
As we have now checked for identical characters at the first two positions of the string so we now make a recursive call without the first character of the string.
The above approach has been implemented below: 
C++
Java
Python3
C#
PHP
Javascript
// Recursive CPP program to insert * between// two consecutive same characters.#include <iostream>using namespace std; // Function to insert * at desired positionvoid pairStar(string& input, string& output,              int i = 0){    // Append current character    output = output + input[i];     // If we reached last character    if (i == input.length() - 1)        return;     // If next character is same,    // append '*'    if (input[i] == input[i + 1])        output = output + '*';           pairStar(input, output, i+1);} // Driver codeint main(){    string input = "geeks", output = "";    pairStar(input, output);    cout << output << endl;    return 0;}
// Recursive Java program to insert * between// two consecutive same characters.class GFG{ static String output=""; // Function to insert * at desired positionstatic void pairStar(String input,            int i){    // Append current character    output = output + input.charAt(i);     // If we reached last character    if (i == input.length() - 1)        return;     // If next character is same,    // append '*'    if (input.charAt(i) == input.charAt(i+1))        output = output + '*';         pairStar(input, i+1);} // Driver codepublic static void main(String[] args){    String input = "geeks";    pairStar(input,0);    System.out.println(output);}} // This code is contributed by Princi Singh
# Recursive CPP program to insert * between# two consecutive same characters. # Function to insert * at desired positiondef pairStar(Input, Output, i = 0) :         # Append current character    Output = Output + Input[i]     # If we reached last character    if (i == len(Input) - 1) :        print(Output)        return;     # If next character is same,    # append '*'    if (Input[i] == Input[i + 1]) :        Output = Output + '*';     pairStar(Input, Output, i + 1); # Driver codeif __name__ == "__main__" :     Input = "geeks"    Output = ""    pairStar(Input, Output);     # This code is contributed by Ryuga
// Recursive C# program to insert * between// two consecutive same characters.using System;     class GFG{ static String output=""; // Function to insert * at desired positionstatic void pairStar(String input,            int i){    // Append current character    output = output + input[i];     // If we reached last character    if (i == input.Length - 1)        return;     // If next character is same,    // append '*'    if (input[i] == input[i+1])        output = output + '*';         pairStar(input, i+1);} // Driver codepublic static void Main(String[] args){    String input = "geeks";    pairStar(input,0);    Console.WriteLine(output);}} /* This code is contributed by PrinciRaj1992 */
<?php // Recursive PHP program to insert * between// two consecutive same characters. // Function to insert * at desired positionfunction pairStar(&$input, &$output, $i = 0){    // Append current character    $output = $output . $input[$i];     // If we reached last character    if ($i == strlen($input) - 1)        return;     // If next character is same,    // append '*'    if ($input[$i] == $input[$i + 1])        $output = $output . '*';         pairStar($input, $output, $i+1);}     // Driver code    $input = "geeks";    $output = "";    pairStar($input, $output);    echo $output;    return 0;     // This code is contributed by ChitraNayal?>
<script>// Recursive Javascript program to insert * between// two consecutive same characters.     let output=""; // Function to insert * at desired positionfunction pairStar(input,i){    // Append current character    output = output + input[i];       // If we reached last character    if (i == input.length - 1)        return;       // If next character is same,    // append '*'    if (input[i] == input[i+1])        output = output + '*';           pairStar(input, i+1);} // Driver codelet input = "geeks";pairStar(input,0);document.write(output);  // This code is contributed by avanitrachhadiya2155</script>
ge*eks
 
Note: The recursive function in the above code is tail recursive as the recursive call is the last thing executed by the function. 
ankthon
ukasp
princi singh
princiraj1992
avanitrachhadiya2155
tail-recursion
C++ Programs
Recursion
Strings
Strings
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Passing a function as a parameter in C++
Program to implement Singly Linked List in C++ using class
cout in C++
Pi(π) in C++ with Examples
Const keyword in C++
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Program for Sum of the digits of a given number