Combinations of characters

Ashutosh_shukla

Ashutosh_shukla

@ashutosh-shukla-ed4Ei4 Oct 21, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Prasad Ajinkya

    Prasad Ajinkya

    @prasad-aSUfhP Nov 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?
  • Ashutosh_shukla

    Ashutosh_shukla

    @ashutosh-shukla-ed4Ei4 Nov 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();
    }
    
    😁