The below C# program is used to print all the possible way to arrange the characters in the given string. Also known as Permutation.
Program :
Program :
- using System;
- public class Permutation
- {
- public void Permute(string inputString, int startIndex, int endIndex)
- {
- if (startIndex == endIndex)
- Console.WriteLine(inputString);
- else
- {
- for (int i = startIndex; i <= endIndex; i++)
- {
- inputString = Swap(inputString, startIndex, i);
- Permute(inputString, startIndex + 1, endIndex);
- inputString = Swap(inputString, startIndex, i);
- }
- }
- }
- private string Swap(string a, int i, int j)
- {
- char temp;
- char[] arr = a.ToCharArray();
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- return new string(arr);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string inputString = "ABC";
- Permutation p = new Permutation();
- p.Permute(inputString, 0, inputString.Length - 1);
- Console.ReadLine();
- }
- }
Output :
ABC
ACB
BAC
BCA
CBA
CAB
No comments:
Post a Comment