# Move all occurence of letter ‘x’ from the string s to the end using Recursion

Given a string s, our task is to move all the occurrence of letter x to the end of the string s using recursion.

Note: If there are only letter x in the given string then return the string unaltered.

Examples:

Input: s= “geekxsforgexxeksxx”
Output: geeksforgeeksxxxxx
Explanation:
All occurrence of letter ‘x’ is moved to the end.

Input: s = “xxxxx”
Output: xxxxx
Explanation:
Since there are only letter x in the given string therefore the output is unaltered.

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

Approach:

To solve the problem mentioned above we can use Recursion. Traverse in the string and check recursively if the current character is equal to the character ‘x’ or not. If not then print the character otherwise move to the next character until the length of the string s is reached.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to Move all occurence of letter ‘x’ ` `// from the string s to the end using Recursion ` `#include ` `using` `namespace` `std; ` ` `  `// Function to move all 'x' in the end ` `void` `moveAtEnd(string s, ``int` `i, ``int` `l) ` `{ ` `    ``if` `(i >= l) ` `        ``return``; ` ` `  `    ``// Store current character ` `    ``char` `curr = s[i]; ` ` `  `    ``// Check if current character is not 'x' ` `    ``if` `(curr != ``'x'``) ` `        ``cout << curr; ` ` `  `    ``// recursive function call ` `    ``moveAtEnd(s, i + 1, l); ` ` `  `    ``// Check if current character is 'x' ` `    ``if` `(curr == ``'x'``) ` `        ``cout << curr; ` ` `  `    ``return``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"geekxsforgexxeksxx"``; ` ` `  `    ``int` `l = s.length(); ` ` `  `    ``moveAtEnd(s, 0, l); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to Move all occurence of letter ‘x’ ` `// from the string s to the end using Recursion ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `// Function to move all 'x' in the end ` `static` `void` `moveAtEnd(String s, ``int` `i, ``int` `l) ` `{ ` `    ``if` `(i >= l) ` `        ``return``; ` ` `  `    ``// Store current character ` `    ``char` `curr = s.charAt(i); ` ` `  `    ``// Check if current character is not 'x' ` `    ``if` `(curr != ``'x'``) ` `        ``System.out.print(curr); ` ` `  `    ``// recursive function call ` `    ``moveAtEnd(s, i + ``1``, l); ` ` `  `    ``// Check if current character is 'x' ` `    ``if` `(curr == ``'x'``) ` `        ``System.out.print(curr); ` ` `  `    ``return``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``String s = ``"geekxsforgexxeksxx"``; ` ` `  `    ``int` `l = s.length(); ` ` `  `    ``moveAtEnd(s, ``0``, l); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

## Python3

 `# Python3 implementation to move all  ` `# occurrences of letter ‘x’ from the  ` `# string s to the end using recursion ` ` `  `# Function to move all 'x' in the end ` `def` `moveAtEnd(s, i, l): ` ` `  `    ``if``(i >``=` `l): ` `       ``return` ` `  `    ``# Store current character ` `    ``curr ``=` `s[i] ` ` `  `    ``# Check if current character ` `    ``# is not 'x' ` `    ``if``(curr !``=` `'x'``): ` `        ``print``(curr, end ``=` `"") ` ` `  `    ``# Recursive function call ` `    ``moveAtEnd(s, i ``+` `1``, l) ` ` `  `    ``# Check if current character is 'x' ` `    ``if``(curr ``=``=` `'x'``): ` `        ``print``(curr, end ``=` `"") ` ` `  `    ``return` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``s ``=` `"geekxsforgexxeksxx"` `    ``l ``=` `len``(s) ` ` `  `    ``moveAtEnd(s, ``0``, l) ` ` `  `# This code is contributed by Shivam Singh `

## C#

 `// C# implementation to Move all occurence of letter ‘x’ ` `// from the string s to the end using Recursion ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to move all 'x' in the end ` `static` `void` `moveAtEnd(``string` `s, ``int` `i, ``int` `l) ` `{ ` `    ``if` `(i >= l) ` `        ``return``; ` ` `  `    ``// Store current character ` `    ``char` `curr = s[i]; ` ` `  `    ``// Check if current character is not 'x' ` `    ``if` `(curr != ``'x'``) ` `        ``Console.Write(curr); ` ` `  `    ``// recursive function call ` `    ``moveAtEnd(s, i + 1, l); ` ` `  `    ``// Check if current character is 'x' ` `    ``if` `(curr == ``'x'``) ` `        ``Console.Write(curr); ` ` `  `    ``return``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``string` `s = ``"geekxsforgexxeksxx"``; ` ` `  `    ``int` `l = s.Length; ` ` `  `    ``moveAtEnd(s, 0, l); ` `} ` `} ` ` `  `// This code is contributed by Nidhi_Biet `

Output:

```geeksforgeeksxxxxx
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.