By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

about string combination

P: n/a
hi,
I want to implement a function that prints all possible combinations of a
characters in a string,eg. input "123"into a textbox,
add "1","2","3","12","13","23","123",to a listbox,Or "ab" into a textbox,add
"a","b","ab"in a listbox,could anyone help me out ?
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
A long lost art of programming.

You may design this algorithm in two step:
1. get all the combinations of chars in your string.
2. for every combination, compute its possible arragements.

like "123" in first step, we get
"1" "2" "3" "12" "23" "13" "123"

the second step, we try to rearrange every terms, so we have:
"1" "2" "3" "12" "21" "23" "32" "13" "31" "132" "123" "312"....

The algorithm to choose combinations:
for each string, we define a string of bits with the same length of it. if
one char of the string is to be choosed, then the corresponding bit is set
to 1, else 0. so you can see ,every possible combinations correspond to a
binary number. now it's easy to understand the seudo codes below:
Combination(char * s)
{
int t=slen(s);//get its length.
for(int i=1;i<exp(2,t-1);i++)//searching all possible combinations
{
string trs="";//empty it
for(int j=0;j<t;j++)
if(i&exp(2,j)==1)//find chars with corresponding bit set to 1
trs+=s[j];//add the char into the set
RearrangeAndOutput(trs);
}
}

You may use recurse algorithm to rearrange the set. I think it's easy to
copy one from your text book.

The algorithm is very slow, you'd better redesign it using bit operations
and check for duplicated results in case you input string like "112".
"AsuWoo" <wu*****@msn.com> дʼ
news:OD**************@TK2MSFTNGP09.phx.gbl...
hi,
I want to implement a function that prints all possible combinations of a
characters in a string,eg. input "123"into a textbox,
add "1","2","3","12","13","23","123",to a listbox,Or "ab" into a textbox,add "a","b","ab"in a listbox,could anyone help me out ?

Nov 15 '05 #2

P: n/a
thank you!

but two combinations that differ only in ordering of their characters are
the same combination. in other words "12" is the same as "21",

and another newbie question how to put characters of a string into a string
array?
is there any String function?
Nov 15 '05 #3

P: n/a
Consulting http://www.freevbcode.com/ShowCode.Asp?ID=2221 might help.

This sort of problem can be researched via Google, with the query "generate
all combinations". Any good book on combinatorial algorithms will also
contain the information.
"AsuWoo" <wu*****@msn.com> wrote in message
news:OD**************@TK2MSFTNGP09.phx.gbl...
hi,
I want to implement a function that prints all possible combinations of a
characters in a string,eg. input "123"into a textbox,
add "1","2","3","12","13","23","123",to a listbox,Or "ab" into a textbox,add "a","b","ab"in a listbox,could anyone help me out ?

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.