In this post we will learn about how to remove character from the string using recursion.
Implementation Using Java:
class RemoveEle
{
public static String removeX(String str)
{
if(str.isEmpty())
{
return str;
}
String ans=" ";
if(str.charAt(0)!='x')
{
ans=ans+str.charAt(0);
}
String small=removeX(str.substring(1));
return ans+small;
}
public static void main(String [] agrs)
{
String str="xaxbxc";
System.out.println("Original String is"+" "+str);
System.out.println("After removing x the string is:"+" "+removeX(str));
}
}
Output:
Original String is: xaxbxc
After removing x the String is abc
Happy Coding!
Follow us on Instagram @programmersdoor
Join us on Telegram @programmersdoor
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
Follow Programmers Door for more.
Comments