CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 24

Threaded View

  1. #1
    Join Date
    Apr 2003
    Posts
    2

    All Combinations of the alphabet

    OK, so I'm trying to write some C++ code that will generate all combinations, of any size of the letters of the alphabet. so...

    A
    AB
    AC
    AD
    AE

    etc etc etc. the code I have now get a good amount of the combinations, but only keeps the combinations in oreder. ex: it gets ABE but not ACE, it gets CDF, but not CEF. here's what I have so far:

    void main()
    {
    const int NUMITEMS = 26;
    char items[100] = "";
    for(int w=0; w< NUMITEMS; w++)
    {
    items[w] = (char)(start+w);
    }
    items[NUMITEMS] = '\0';
    int num =0;
    char temp[26];
    for(int startPlace=0; startPlace<NUMITEMS; startPlace++)
    {
    for(int length=1; length<NUMITEMS-(startPlace-1); length++)
    {
    for(int last=startPlace+length-1; last<NUMITEMS; last++)
    {
    if(length == 1)
    {
    temp[0] = items[startPlace];
    temp[1] = '\0';
    last = NUMITEMS+1;
    }
    else
    {

    int i=0;
    while(i<length-1)
    {
    temp[i] = items[startPlace+i];
    i++;
    }
    temp[length-1] = items[last];
    temp[length] = '\0';
    }
    //Code to calculate the best answer.


    cout << temp<< "\n";
    }
    }
    }
    }

    can anyone tweak my solution to help me get all of them or give come up with some code that will get them all?

    PS: I'm looking for combinations, not permitations. Order Doesn't matter.

    Thanks!

    Jeff
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured