CrazyEngineers
  • Ashutosh_shukla

    Ashutosh_shukla

    MemberNov 20, 2008

    Combinations of characters

    Can someone give me a code that works as follows:

    Input :
    A word containing at max 10 letters.eg: "team"

    Output:
    The program should print the different words that can be formed using these letters.
    eg:team,tema,taem,tame,tmae,tmea, and so on. So the 4 letters give 4! that is 24 words.

    I am stuck up badly as I cant get any idea.Please someone help.😕
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Prasad Ajinkya

    MemberNov 21, 2008

    Ashutosh,
    Here's a hint - Use recursion or while loop.

    You need to -
    1. first divide the string into an array of characters ... so "team" becomes 't','e','a','m'
    2. Then just make a recursive function to output the letter

    Get it?
    Are you sure? This action cannot be undone.
    Cancel
  • Ashutosh_shukla

    MemberNov 24, 2008

    Thanks your advice worked.I got some weird looking code but it worked in some cases.Can you please check it out.
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    char str[10];
    static int index[10];
    int len;
    int count=0;
    void anagram()
    {
     if(count==len-1)
     {
      int p,q,flag;
      index[count]=0;
      for(;index[count]<len;index[count]++)
      {
       flag=1;
       for(p=0;p<=count;p++)
       {
        for(q=p+1;q<=count;q++)
        {
         if(index[p]==index[q])
          flag=0;
        }
       }
       if(flag)
       {
        for(p=0;p<len;p++)
        {
         cout<<str[(index[p])];
        }
        cout<<endl;
        getch();
       }
      }
     }
     else
     {
      count++;
      index[count-1]=0;
      for(;index[count-1]<len;index[count-1]++)
      {
       anagram();
      }
      count--;
     }
    }
    void main()
    {
     clrscr();
     cout<<"Enter the word for which anagrams are to be found out : ";
     cin>>str;
     len=strlen(str);
     cout<<"\nThe anagrams found are as follows : \n";
     cout<<"Press enter to get next anagram : \n";
     anagram();
     getch();
    }
    
    😁
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register