#include<iostream>
#include<cstring>
usingnamespace std;
void char_permutation(char str[],char append[])
{
int length = strlen(str);
if (length)
{
for(int i=0;i<length;++i)
{
char* str1 = newchar[length+1];
int cnt;
int cnt2;
for(cnt=0,cnt2=0; cnt<length; ++cnt,++cnt2)
{
if (cnt == i)
{
str1[cnt] = str[++cnt2];
continue; // Is this continue necessery here does it make a difference?
}
else
str1[cnt] = str[cnt2];
}
str1[cnt] = '\0';
int alength = strlen(append);
char* append1 = newchar [alength+2];
strncpy(append1,append,alength);
append1[alength] = str[i];
append1[alength+1] = '\0';
char_permutation(str1,append1); //Function calls itself
delete []str1; //Will this be done after the function calls itself
delete []append1;
}
}
else
{
cout << append << endl;
}
}
int main()
{
char str[] = "BUSHES";
char append[] = "\0";
cout << "Original = " << str << endl;
char_permutation(str,append);
cout << "Done ........" << endl;
system("PAUSE");
return 0;
}
This program generates all permutations for entered characters.
Could someone please:
1) Tell me how this works
2) When the function calls itself will it finish whats written after the calling of itself or will it skip what comes after calling itself (see comments in code)
3) How can I modify this code so it generates permutations with 3, 4, 5 places even if the str is 6 chars long. Like char str[] = BUSHES and it generate BUS BUSH BUSEH not only BUSHES BUSHSE and so on.
what about the first loop will it continue or will it stop and begin all over again since you called the function? If both are executed which one will be executed firtst?
There's nothing special about recursive functions, it will be first executed the innermost call and then the outer calls will continue normally ( after the inner one has completed ).
Try this to understand:
1 2 3 4 5 6 7 8 9 10 11
void recursive ( int num )
{
if ( num <= 0 )
{
cout << "The deepest function has been called\n";
return;
}
cout << "Funtion " << num << " begins\n";
recursive ( num-1 );
cout << "Function " << num << " ends" << endl;
}
That's quite easy, create new strings of the desired length, fill it with the starting word characters you want and pass the new string to the function
But if I enter 6 chars then i have to make 20 strings for the words with 3 letters 60 strings with 4 and 120 with 5. Or is it I who doesent understand your answer? If it is so could you please post a piece of source then i might understand you better
I want it to make all possible permutations from it the code abover only generates thos by using all characters in a six character word. I want it also to generate permutations using six characters in a three letter word like
BUS
BSU
UBS
USB
SBU
SUB
BUH
BHU
UBH
UHB
HBU
HUB
BUE
BEU
UBE
UEB
EBU
EUB
BUS
BSU
UBS
USB
SBU
SUB
BSH
BHS
SBH
SHB
HBS
HSB
BSE
BES
SBE
SEB
EBS
ESB
BSS
BSS
SBS
SSB
SBS
SSB
BHE
BEH
HBE
HEB
EBH
EHB
BHS
BSH
HBS
HSB
SBH
SHB
BES
BSE
EBS
ESB
SBE
SEB
USH
UHS
SUH
SHU
HUS
HSU
USE
UES
SUE
SEU
EUS
ESU
USS
USS
SUS
SSU
SUS
SSU
UHE
UEH
HUE
HEU
EUH
EHU
UHS
USH
HUS
HSU
SUH
SHU
UES
USE
EUS
ESU
SUE
SEU
SHE
SEH
HSE
HES
ESH
EHS
SHS
SSH
HSS
HSS
SSH
SHS
HES
HSE
EHS
ESH
SHE
SEH
ESS
ESS
SES
SSE
SES
SSE
Some are repeating because there are two S in characters in "BUSHES" i want to do same for 3 4 and 5 letters
OK, yo need to have an array which contains three letters and fill it with some sort of loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
char perm3 [4]; // 3 letters + \0
perm3[3] = '\0';
for ( int first = 0; first < strlen(str)-2; first++ ) // iterates B U S H
{
perm3[0] = str[first];
for ( int second = first+1; second < strlen(str)-1; second++ ) // first time iterates U S H E
{
perm3[1] = str[second];
for ( int third = second; third < strlen(str); third++ ) // first time iterates S H E S
{
perm3[2] = str[third]; // now you should get all possible combinations of 3 letters
char_permutation( /*whatever goes in here*/ );
}
}
}
This is the output I get with your program. Not the same as i wanted. It doesent make all combinations and it creates combinations using same letters in it
BUU
BUS
BUH
BUE
BUS
BSS
BSH
BSE
BSS
BHH
BHE
BHS
BEE
BES
USS
USH
USE
USS
UHH
UHE
UHS
UEE
UES
SHH
SHE
SHS
SEE
SES
HEE
HES
I Figured it out... Now the only thing is to apply a SendKeys function. No need for help with SendKeys I found something on the internet. The code generates all permutations from three letters to how many you entered