Wednesday, March 30, 2016

C program to print all the posstion permutations of the string

#include<stdio.h>
#include<conio.h>
#include<string.h>
int factorial(int x)
 {
  if(x==1||x==0)
   {
    return 1;
   }
  else
   {
    return x*factorial(x-1);
   }
 }
void main()
{
 clrscr();
 char str[]="ABCD";
 int i,j,k,l,count=0;
 char temp;
 /*for(i=0;i<4;i++)
  {
   for(j=0;j<3;j++)
    {
     for(k=0;k<2;k++)
      {
       for(l=0;l<1;l++)
{
printf("%d %d \n",i,j);
count=count+1;
}
      }
    }
  }*/
  l=strlen(str);
  j=0;
  k=1;
  for(i=0;i<factorial(l);i++)
   {
    if(j==l)
     {
      j=0;
     }
    if(k==l)
     {
      k=0;
     }
    temp=str[j];
    str[j]=str[k];
    str[k]=temp;
    printf("%s\n",str);
    j++;
    k++;
   }
 //printf("count %d",count);
 getch();
}

output

No comments:

Post a Comment