Related Articles
Recursive program to insert a star between pair of identical characters
• Difficulty Level : Medium
• Last Updated : 10 Jul, 2019

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
```

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

Approach:

• If there is an empty string then simply return. This forms our base condition.

Else we do the following-

• Check if the first two characters are identical. If yes, then insert ” * ” between them.
• As we have now checked for identical characters at 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++

 `// Recursive CPP program to insert * between``// two consecutive same characters.``#include ``using` `namespace` `std;`` ` `// Function to insert * at desired position``void` `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 code``int` `main()``{``    ``string input = ``"geeks"``, output = ``""``;``    ``pairStar(input, output);``    ``cout << output << endl;``    ``return` `0;``}`

## Java

 `// Recursive Java program to insert * between``// two consecutive same characters.``class` `GFG ``{`` ` `static` `String output=``""``;`` ` `// Function to insert * at desired position``static` `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 code``public` `static` `void` `main(String[] args) ``{``    ``String input = ``"geeks"``;``    ``pairStar(input,``0``);``    ``System.out.println(output);``}``}`` ` `// This code is contributed by Princi Singh`

## Python3

 `# Recursive CPP program to insert * between ``# two consecutive same characters. `` ` `# Function to insert * at desired position ``def` `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 code ``if` `__name__ ``=``=` `"__main__"` `:`` ` `    ``Input` `=` `"geeks"``    ``Output ``=` `""``    ``pairStar(``Input``, Output); ``     ` `# This code is contributed by Ryuga`

## C#

 `// Recursive C# program to insert * between``// two consecutive same characters.``using` `System;``     ` `class` `GFG ``{`` ` `static` `String output=``""``;`` ` `// Function to insert * at desired position``static` `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 code``public` `static` `void` `Main(String[] args) ``{``    ``String input = ``"geeks"``;``    ``pairStar(input,0);``    ``Console.WriteLine(output);``}``}`` ` `/* This code is contributed by PrinciRaj1992 */`

## PHP

 ``
Output:
```ge*eks
```

Note:The recursive function in the above code is tail recursive as recursive call is the last thing executed by the function.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.

My Personal Notes arrow_drop_up