0

I am trying to do the following on my computer but not getting it to perform, like say I have a name of a person and I want to make a different combinations of the letters in his name:

NAME ABC
                                       ABC
                                 /      |      \
                               A        B        C
                              /|\     / | \     /| \
                            AA AB AC BA BB BC CA CB CC 
                             .           .        .
                             .           .        .

I want to make combinations of the above name, for example:

ABC A B C AA AB AC BA BB BC CA CB CC.... AAA... BBB... CCC...

How can I do this in C++?

I wrote the following code for it:

 string c = "ABC";
 for (i = 0; i < c.length(); i++)
     c.at(i);

But it only generated A, B, C. How do I generate AB, AA, AC, ...?

5
  • 2
    Consider writing an algorithm in pseudo-code to get a clear understanding how you plan to solve it first. Then you can translate it into C. Commented Nov 1, 2013 at 4:42
  • 1
    What about CBA, BAC, etc.? Or did you just want two-letter combinations? The good news is the code you have is the building block for the rest of the algorithm. You definitely need to write some pseudocode or even draw on paper what you think should happen. Commented Nov 1, 2013 at 4:46
  • I don't get it if you are allowing repetition of characters then the number of possible words is infinite. Commented Nov 1, 2013 at 5:15
  • check it now its i think clear Commented Nov 1, 2013 at 5:34
  • Try a simple permutation algorithm Commented Nov 1, 2013 at 5:50

2 Answers 2

3

you can do this

string c='ABC';
int n=c.length();

for(int i=0;i<n;i++)
{
  std::cout<<c[i]<<" ";

  for(int j=0 ; j< n ;j++)
  {
      std::cout<<c[i]<<c[j]<<" ";
  }
}

The output is: A AA AB AC B BA BB BC C CA CB CC

If you want the three letter combinations, add a third for loop inside the second for loop with the same end points like

for(int k=0;k<n;k++) and inside the for loop cout<<c[i]<<c[j]<<c[k]<<" "

Sign up to request clarification or add additional context in comments.

1 Comment

you are right but i want it general like if i go for 3 combinations or 4 combinations i.e AAA,AAB, AAC or AAAA AAAB AAAC and so on store it in a string
2

Here's a solution involving lovely recursive templates:

#include <iostream>
#include <string>

template <int Depth>
void print_combinations(const std::string& name, const std::string& prefix = "")
{
    for (int i=0; i < name.size(); i++) {
        std::cout << prefix << name[i] << " ";
        print_combinations<Depth - 1>(name, prefix + name[i]);
    }
}

template <>
void print_combinations<0>(const std::string&, const std::string&)
{
}

int main()
{
    std::string name = "ABC";

    print_combinations<4>(name);
}

For Depth=4 (as above), it prints

A AA AAA AAAA AAAB AAAC AAB AABA AABB AABC AAC AACA AACB AACC AB ABA ABAA ABAB ABAC ABB ABBA ABBB ABBC ABC ABCA ABCB ABCC AC ACA ACAA ACAB ACAC ACB ACBA ACBB ACBC ACC ACCA ACCB ACCC B BA BAA BAAA BAAB BAAC BAB BABA BABB BABC BAC BACA BACB BACC BB BBA BBAA BBAB BBAC BBB BBBA BBBB BBBC BBC BBCA BBCB BBCC BC BCA BCAA BCAB BCAC BCB BCBA BCBB BCBC BCC BCCA BCCB BCCC C CA CAA CAAA CAAB CAAC CAB CABA CABB CABC CAC CACA CACB CACC CB CBA CBAA CBAB CBAC CBB CBBA CBBB CBBC CBC CBCA CBCB CBCC CC CCA CCAA CCAB CCAC CCB CCBA CCBB CCBC CCC CCCA CCCB CCCC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.