ANAGRAM WITH RECURSION
Java Program to print all anagram using recursion
Computer generated Outputs:
Coding is:-
import java.io.*;
class rea
{int c,d;
void display(String s1,String s2)
{ if(s2.length()<=1)
{c++;d++;
if(c==10)
{System.out.println(s1+s2);c=0;
}
else
System.out.print(s1+s2+" ");
}
else
{ for(int i=0;i<s2.length();i++)
{String x=s2.substring(i,i+1);
String y=s2.substring(0,i);
String z=s2.substring(i+1);
display(s1+x,y+z);
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print(" Enter String : ");
String s=br.readLine();
rea ob=new rea();
ob.display("",s);
System.out.println("\n\n\nTotal number of anagrams = "+ob.d);
}
}
Computer generated Outputs:
Coding is:-
import java.io.*;
class rea
{int c,d;
void display(String s1,String s2)
{ if(s2.length()<=1)
{c++;d++;
if(c==10)
{System.out.println(s1+s2);c=0;
}
else
System.out.print(s1+s2+" ");
}
else
{ for(int i=0;i<s2.length();i++)
{String x=s2.substring(i,i+1);
String y=s2.substring(0,i);
String z=s2.substring(i+1);
display(s1+x,y+z);
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print(" Enter String : ");
String s=br.readLine();
rea ob=new rea();
ob.display("",s);
System.out.println("\n\n\nTotal number of anagrams = "+ob.d);
}
}
Comments
Post a Comment