Monday, December 21, 2009

Permutation of string characters


// Permutation.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


#include "string.h"


 


char in[100],out[100];


int used[100],count=0;


int _tmain(int argc, _TCHAR* argv[])


{


      int len,j;


      void permute(int len,int level);


      printf("Enter the string: ");


      scanf("%s",in);


      //printf("%s",in);


      len=strlen(in);


      for(j=0;j<len;j++)


      {


            used[j]=0;


      }


      permute(len,0);


      return 0;


}


void permute(int len,int level)


{


      int k,i=0;


      if(level==len)


      {


            for(k=0;k<count;k++)


            {


                  printf("%c",out[k]);


            }


            printf("\n");


            return;


      }


      else


      {


            //printf("In permute");


            //printf("\n%d,%d",i,level);


            for(i=0;i<len;i++)


            {


                  //printf("In for");


                  if(used[i]==1)


                  {


                        //printf("\nIn if");


                        continue;


                  }


                  //printf("\nIn else");


                  out[count]=in[i];


                  count++;


                  used[i]=1;


                  permute(len,level+1);


                  used[i]=0;


                  count--;


            }


      }


}


 


Enter the string: abc
abc
acb
bac
bca
cab
cba
Press any key to continue . . .

No comments:

Post a Comment