// Assign all the keys an index from the stringfor(i=1;i<=key[0];i++) key[i]=i;

display(key,str); // Displays the string

swap(key,key[0],key[0]-1); // swaps the key values n and n-1

display(key,str); // Displays the string

// Added by Shabbir// store the factorial once and so it does not calulate each timeunsignedlong num = fact(key[0]);

// Loop till factorial n so we get all the combination of strings// It starts with i=3 as we already have 2 combination // Its increments by 2 as we get 2 combination with the help of swapfor(i=3;i<=num;i+=2){// Loop for all the elements of the stringfor(j=key[0]-1;j>=0;j--){// Edited by Shabbir// Can be avoided//if ((i-1)%fact(j)==0){ Add(key,key[0]-j); // Increment the key in a cyclic formfor(k=1;k<=key[0]-j-1;k++){// If kth index in key is euqal to n-jth indexif(key[k]==key[key[0]-j]){ Add(key,key[0]-j); // Increment the key in a cyclic form k=0;}}}} display(key,str); // Displays the string swap(key,key[0],key[0]-1); // swaps the key values display(key,str); // Displays the string}cout << "\nTotal no: of combinations = " << key[0] << "! = " << i-1; cout<<endl; getch();}