CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Dec 2011
Posts
19

## Combination of characters.

Well suppose I got a String "ABC", I need to put up all possible combinations.

Eg. AAA, AAB, AAC, ABA, ABB.....etc

I need a non-recursive technique, and the code should work for Strings of length less than 30.

Anybody with a solution?

2. Member
Join Date
Aug 2009
Location
NW USA
Posts
173

## Re: Combination of characters.

What do you have so far?

3. ## Re: Combination of characters.

Hmm...well its kinda like counting don't you think ? Imagine we only had 4 symbols for representing basic numbers:-
Code:
```000
001
002
003
010
011
012
013
100
101
102
........```
Try implementing an algorithm based on that.

4. Member
Join Date
Aug 2009
Location
NW USA
Posts
173

## Re: Combination of characters.

This is kool without recursion. There are actually N^N posibilites. I would create that many arrays of char with length N (a 1-dim array of string would probably work too with appending). Then you need to loop through your input string. Each letter will be repeated the same number of times as N raised to the output position-1.

input: abc (N=3, 27 possible words)

1 2 3 <-position
1 3 9 <-repeats N^(position-1)

a a a
b a a
c a a
a b a
b b a
c b a
a c a
b c a
c c a
a a b
b a b
c a b
a b b
b b b
c b b
a c b
b c b
c c b
a a c
b a c
c a c
a b c
b b c
c b c
a c c
b c c
c c c

So you need two loops. One to itterate through the characters and the other to repeat the character depending on it's position in the output.
Last edited by Mur16; March 4th, 2013 at 01:12 PM.

#### Posting Permissions

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