0

I want to generate all possible strings with the given set and length in C#.Net. for example with the set {+,-,0,1,2,3,4,5,6,7,8,9} (not always numbers) and length of 4:

+001 ,
001+ ,
0+01 ,
12+1 ,
02-9 ,
1502 ,
...
tshepang
  • 12,111
  • 21
  • 91
  • 136
Saeed
  • 37
  • 1
  • 6

2 Answers2

5
char[] chars = "+-0123456789".ToCharArray();
var strings =
    from a in chars
    from b in chars
    from c in chars
    from d in chars
    select new string(new[] { a, b, c, d });
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
0

it is not effective but it works fine

   List<char> input;
    public static void Main()
    {
    input=new List<char>(){'+','-','0','1','2','3','4','5','6','7','8','9'};

            List<char> permutation = new List<char>();
            permutation.Add(input[0]);
            permutation.Add(input[0]);
            permutation.Add(input[0]);

                GetPermutation(ref permutation, input.Count); 
}

 private static void GetPermutation(ref List<char> permutation,int length)
        {


            for (int i = 0; i < length; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    for (int k = 0; k < length; k++)
                    {
                        string output = "";
                        permutation[0] = input[k];
                        permutation[1] = input[j];
                        permutation[2] = input[i];
                        output += permutation[0];
                        output += permutation[1];
                        output += permutation[2];
                        Console.WriteLine(output + "\n");
                        count++;
                    }
                }

            }

        }
HackerMan
  • 904
  • 7
  • 11