Tuesday, July 14, 2009

Combination of String in C++?

i wish to get different combination from a String. For example: String^ str = "a,b,c,d,e";


i will wan to separate the string into individual letters and form all different combination like (a,b), (a,c,d) or (a,b,c,d) etc..


is there any simple methods to do so juz using loop?? im still a beginner in C++ so i dun really noe any built in methods. But if haf i will wish to learn too. pls help!! thx

Combination of String in C++?
I know of no easy way of doing the task you want to accomplish using a built in library function of C/C++. Basically, there are a few things you will need to do:





1. Parse out each of the comma separated characters and create a set containing each of those characters (this set could be stored in an array of chars).





2. Print the power set of the character set you have. The power set is the formal name for what you are looking to print. A simple algorithm for this would be to start with the first element in your list and combine that with each of the characters after it. Once all combinations involving the first character have been exhausted, move on to the second but only do combinations of the second not involving the first character which came before it. Do this for each character in the set and your task will be accomplished.





This problem is ripe for recursion if you are so inclined.


No comments:

Post a Comment