We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Wednesday, March 27, 2019

Permutations of string like XYZ in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructureExam
{
    public class StrPre
    {
        public static void Main()
        {
            String str = "xyz";
            int n = str.Length;
            permute(str, 0, n - 1);
            Console.ReadLine();

        }
        public static void permute(string str, int first, int last)
        {
            if (first == last)
            {
                Console.WriteLine(str);
            }
            else
            {
                for (int i = first; i <= last; i++)
                {
                    char temp;
                    char[] charArray = str.ToCharArray();
                    temp = charArray[first];
                    charArray[first] = charArray[i];
                    charArray[i] = temp;
                    string str1 = new string(charArray);

                    permute(str1, first + 1, last);
                }
            }
        }

    }

}

Output:

No comments: