Professional Documents
Culture Documents
Assignment 14
Write it in your practical notebook with algorithm and variable description
Question 14: 21/08/23
/*Write a program to generate all possible anagram of the word.
If the given word is 'dop' then there will be six possible words combinations.
An anagram must be printed only once. You may output any order.
Also output the total no. of anagram. you may assume the total no. of
words will be less than or equal to 7.*/
import java.util.*;
class anagrams
{
static int c;
static void comb(String str, int l, int r)
{
if (l == r)
{
System.out.println(str);
c++;
}
else
{
for (int i = l; i <= r; i=i+1)
{
str = swap(str,l,i);
comb(str, l+1, r);
str = swap(str,l,i);
}
}
}
static String swap(String a, int i, int j)
{
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
public static void main()
{
Scanner in=new Scanner (System.in);
c=0;
int n,i;
String str="";
System.out.println("Enter a word upto 7 letters");
str=in.next();
n=str.length();
if(n>=1 && n<=7)
{
comb(str, 0, n-1);
System.out.println("number of combinations = "+c);
}
else
{
System.out.println("Invalid input");
}
}
}
/*INPUT/OUTPUT