Page 417 - Computer science 868 Class 12
P. 417

Program 8      Design a program using recursive technique to generate all possible anagrams of a word.
                                 Assume that the word is entered in upper case with length maximum of seven characters and
                                 there are no repeating alphabets in the word. An anagram must be printed only once. Also,
                                 count the total number of anagrams generated.                     [ISC Practical 2005]
                                 Say the word is TOP then the anagrams are
                                 TOP, TPO, OPT, OTP, PTO, POT
                                 Total number of anagrams = 6


                   1      import java.util.*;
                   2      class Anagram

                   3      { String str;
                   4       int freq;

                   5       void accept() // input word
                   6       { Scanner sc=new Scanner(System.in);
                   7        boolean f=true;

                   8        int c=0,l;

                   9        do{  // validating word
                  10        f=true;
                  11        System.out.println("Enter word in upper case");

                  12        str=sc.next().toUpperCase();
                  13        l=str.length();

                  14         if(l>7)
                  15        { f=false;

                  16           System.out.println(str+" length>7");
                  17        }

                  18        for(int i=65;i<=90;i++)
                  19        { c=0;

                  20         for(int j=0;j<l;j++)
                  21         { if((char)i==str.charAt(j))

                  22           c++;
                  23         }

                  24         if(c>1)
                  25         { f=false;

                  26          System.out.println(str+" has repeating alphabets");
                  27          break;

                  28         }




                                                                                                                       415
                                                                                                           Recursion   415
   412   413   414   415   416   417   418   419   420   421   422