Friday, 21 February 2014

write Program To Print All Permutations of given string

Combination programs require good understanding of mathematics . In combination or permutation concepts some formulas are used to identify how this combination of letters will be formed and how many combinations will be made . 

Formula for Permutation :- 
                                                N! / (N-r)!
Where N is the total number of characters that are available in your input string . 
Where r is total number of characters that will be taken at one time .

 Like if total number of characters in string is '5 ' and total number of characters taken at one time is '4' Then

Permutation = 5! / (5-4)!  = 120


Formula for Combination :- 
                                                      N! / r!  * (N-r)!
                                                 
Where N is the total number of characters that are available in your input string . 
Where r is total number of characters that will be taken at one time .

Program
In the following program we are making a program in C++ to print all possible combinations of  "abcd" string . As we can see we have to find permutation . So formula for it is :-

    N! / (N-r)!
    4! / (4-4)!  =24 
So 24 total combinations will be made from string "abcd" . There are four characters and 24 combinations . so each character will remain at one position for 24/4 time means 6 times. 

Program To Print all possible combinations of all characters of  "abcd" string .


#include< iostream.h >

#include< conio.h >
main ( )
{
clrscr ( );
int n=4;        // Here n is 4 so this programs prints all possible combinations for
char a[5];      // 4 character you can specify any value you want
cout<<"This program gives all possible combinations of four characters "<<endl;
cout<<"\nEnter Four Characters :-  ";

//Input four character from user in a [  ] array
for(int x=0;x<=3;x++)
{
 cin>>a[x];
}

cout<<"\nNow Combinations "<<" is :- \n"<<endl;

for(int z=0;z<=n-1;z++)
{
for(int j=0;j<=n-2;j++)
{
int temp;
temp=a[n-1];
a[n-1]=a[n-2];
a[n-2]=temp;
cout<<"\t";

for (int l=0;l<=n-1;l++)
{
 cout<<a[l];
 }

cout<<"\t";

temp=a[n-3];
a[n-3]=a[n-1];
a[n-1]=temp;

for ( int l2=0;l2<=n-1;l2++)
 {
cout<<a[l2];
 }
 }

 int temp1;
 temp1  =  a[0];
 a[0]  =  a[z+1];
 a[z+1]  =  temp1;

 cout<<"\n";
  }
getch();

}


OUTPUT :-



CODE EXPLANATION

cout<<"\nEnter Four Characters :-  ";

//Input four character from user in a [  ] array
for(int x=0;x<=n;x++)
{
 cin>>a[x];
}


In This Block of code we are getting input of four character whose combinations are generated by this program. In this we have used a for loop that consist of x counter that is initialized from 0 and it will continue upto 3 that allows user to enter 4 character to get their all possible combinations

Code below is logic of this program that given all possible combinations of entered characters
I have explained it with comments with it



for(int z=0;z<=n-1;z++)    // loop continues from 0 to 3 (  if n is 4 )
{
for(int j=0;j<=n-2;j++)       // loop continues from 0 to 2 (  if n is 4 )
{
{
int temp;
temp=a[n-1];
a[n-1]=a[n-2];
a[n-2]=temp;
cout<<"\t";

for (int l=0;l<=n-1;l++)
{
 cout<<a[l];
 }

cout<<"\t";

temp=a[n-3];
a[n-3]=a[n-1];
a[n-1]=temp;

for ( int l2=0;l2<=n-1;l2++)
 {
cout<<a[l2];
 }
 }

 int temp1;
 temp1  =  a[0];
 a[0]  =  a[z+1];
 a[z+1]  =  temp1;

 cout<<"\n";
  }

This code of block produces the all possible combinations of given input character by placing each character at all positions within the string each character including its original position within string will exchanged with or replaced with every position within the string so as to generate unique string each time

Share this

0 Comment to "write Program To Print All Permutations of given string"

Post a Comment