Program to find all the permutations of the string


#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
void permutation(char *a, int i, int n)
{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a[i]), (a[j]));
          permute(a, i+1, n);
          swap((a[i]), (a[j]));
       }
   }
}
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int main()
{
char a[] = "ABCD";
   permutation(a, 0, 3);    // start index-0 and last index-3 of an array
   getch();
}

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Programs and Puzzles in technical interviews i faced