C# Program to find all possible arrangements of a given string

Quasar S. Chunawalla
This program accepts a string from the user and finds all possible arrangement of the letters that make up the string. For example, if you enter TAP The possible arrangements are 6 : TAP TPA APT ATP PAT PTA The program is given in the listing below –
class Program { static void Main(string[] args) { Console.WriteLine("Enter a string : "); string str = Console.ReadLine(); char[] A = str.ToCharArray(); int l = A.Length; int[] indexes = new int[l]; int count = 0; int flag; /*The value of count is l to the power l*/ int m = 1; int maxCount = 1; while (m <= l) { maxCount = maxCount * l; m = m + 1; } while(count<maxCount) { count++; flag = 0; //Increment the indexes counter indexes[l - 1] = indexes[l - 1] + 1; //Make sure that if the digits over the subsequent higher digit is updated for (int j = 1; j <= l - 1; j++) { if (indexes[l - j] == l)

{ indexes[l - j - 1]++; indexes[l - j] = 0; } }

/*Check if any pair from all possible pairs contain the same digits*/ for (int x = 0; x < l - 1; x++) { for (int y = x + 1; y < l; y++) { if (indexes[x] == indexes[y]) { flag = 1; break; } } } if (flag == 0) { Console.WriteLine(); /*Display the indexes array*/ for (int k = 0; k < l; k++) { Console.Write(A[indexes[k]] + " } } } } }

");