0

I need to make a combinaion of Stings "a" "b" "c" "d". I've tried putting them in a list then parsing a foreach methord through them, but to no avail.

What else can I do?

5
  • You mean to concatenate them ? Commented Jun 8, 2011 at 9:23
  • what do you mean by "combination"? Commented Jun 8, 2011 at 9:23
  • 2
    What do you mean to make a combination? Commented Jun 8, 2011 at 9:24
  • I want to get a program that outputs something like "abcd" "abdc" "acdb" "dacb" ect ect ect... Commented Jun 8, 2011 at 9:25
  • 3
    It seems that what you are looking for is actually permutations, not combinations. Commented Jun 8, 2011 at 9:28

6 Answers 6

3

Having the strings in an array you can use the function below to print all permutations so it outputs: abcd, abdc, adbc, etc.

Recursive permutation (from MSDN):

public static void Permute(string[] strings, int start, int finish) { if (start == finish) { for (int i = 0; i <= finish; ++i) { Console.Write(strings[i] + " " ); } Console.WriteLine(""); } else { for (int i = start; i <= finish; ++i) { string temp = strings[start]; strings[start] = strings[i]; strings[i] = temp; Permute(strings, start+1, finish); temp = strings[start]; strings[start] = strings[i]; strings[i] = temp; } } } // Permute() 
Sign up to request clarification or add additional context in comments.

Comments

0

I will give you the logic.

Create a List<String> and then go on adding each string to it.

List<String> s = new List<String>(); s.add("a"); s.add("b"); s.add("c"); s.add("d"); 

Once you added all the strings, then generate a random number between minimum and maximum index like this :

private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } 

Then, while printing each string in the loop over the List with this number, be sure to check that the same random number is not repeated for the nex iteration.

2 Comments

How would I add strings to the list string?
List<String> is a generic type, it will allow only strings to be added, no other object,
0

Will a list do you?

List<String>myStrings = new List<String>(); myStrings.Add("a"); ... myStrings.Add("d"); 

You should be able to loop through that

Comments

0

if you have a set number of strings you could use

var s = String.Format("{0}{1}{2}{3}", stringA, stringB, stringC, stringD); 

otherwise a for/foreach loop would be the way forward,

var sb = new StringBuilder(); var strings = new List<string>(); // Add strings to list for (var i = 0; i < strings.Count; i++) { sb.Append(strings[i]); } var s = sb.ToString(); 

I wouldn't use string + string + string style concatenation as this is bad practice due to the way strings work in memory.

EDIT: I haven't tested the code it was written in the browser! let me know if you have any issues.

Just seen the comments above, the code I have posted will always output the strings in the same order so may not be what you want.

HTH

OneShot

Comments

0
List<String>stringList = new List<String>(); stringList.Add("a"); stringList.Add("b"); ... ... foreach(string item in stringList) { string text=item; } 

Comments

0

This example does all the combinations (proper):

using System; using System.Linq; using System.Collections.Generic; public static class Program { public static void Main(string[] args) { var list = new [] { "a", "b", "c", "d" }; foreach (var combi in Enumerable.Repeat(list, list.Length).CartesianProduct()) Console.WriteLine(string.Join(" ", combi)); } static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) { IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; return sequences.Aggregate( emptyProduct, (accumulator, sequence) => from accseq in accumulator from item in sequence select accseq.Concat(new[] {item})); } } 

Output:

a a a a a a a b a a a c a a a d a a b a a a b b a a b c a a b d a a c a a a c b .... d d b c d d b d d d c a d d c b d d c c d d c d d d d a d d d b d d d c d d d d 

If you needed permutations, you can drop-in an algorithm from

  1. MoreLinq
  2. CodeProject http://www.codeproject.com/KB/recipes/Combinatorics.aspx

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.