473,789 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about string combination

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","12 3",to a listbox,Or "ab" into a textbox,add
"a","b","ab "in a listbox,could anyone help me out ?
Nov 15 '05 #1
3 3180
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(cha r * 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
RearrangeAndOut put(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.co m> дÈëÓʼþ
news:OD******** ******@TK2MSFTN GP09.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","12 3",to a listbox,Or "ab" into a textbox,add "a","b","ab "in a listbox,could anyone help me out ?

Nov 15 '05 #2
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
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.co m> wrote in message
news:OD******** ******@TK2MSFTN GP09.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","12 3",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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
2516
by: Ken Human | last post by:
I want to generate every possible 16 character combination of the characters 0-9, A-Z, and a-z programatically. My current code follows: #include <stdio.h> #include <ctype.h> int main() { char strBuf = {0}; int i, j;
19
9522
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
2
2987
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3 types of items in the very hierarchical data model: - Classes (and nested classes) - Collections - Properties I've successfully written the Reflection code to handle any combination of classes and properties but I'm confused about what to do...
9
7136
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
33
3171
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
13
3227
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
2
2041
by: Jim Heavey | last post by:
I have an SQL statement which is quite long. I want to declare a string variable with this SQL statement and I want to have span multiple lines. I could scrunch it all up and place it on a single line, but then the SQL statement would be unreadable. There was a way to do this in VB, but how do you do this is C#? I want it to look something like this in the code. string mySQL = "select 'Anchor' as Anchor_NonAnchor,...
9
3757
by: paragpdoke | last post by:
Hello All. I'm looking for some algorithm to build a combination of strings from multiple arrays. Let me explain in detail. - I'm working on VBA (excel). I have functions that accept one string and return a collection. These get listed in columns in excel (one column per call to the function). - This results into arrays of strings in different columns. Unfortunately, the number of strings in an array is unknown at design time (these get...
20
3001
by: sophia | last post by:
Dear all, The following is the program which i have done to find all the combination of letters in the string "hello" #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 5
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.