Problems understanding source code

I have a problem with understanding the source.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<cstring>

using namespace std;

void char_permutation(char str[],char append[])
{
int length = strlen(str);
if (length)
{
for(int i=0;i<length;++i)
{
char* str1 = new char[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 = new char [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.
Last edited on
1
2
3
char_permutation(str1,append1); //Function calls itself

delete []str1;              //Will this be done after the function calls itself 

No, it will execute the inner function call and then execute the code after it
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?
Last edited on
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;
}

This article may also help: http://www.cplusplus.com/forum/articles/2231/
I figured it out myself when i had time. No one has answered my third question!!! Please answer it, its the most important...
Last edited on
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
Can you give an example of what you want?
You previously had the example with "BUSHES", which strings do you want from it?
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*/ );
       }
    }
}
See if it works
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 managed to make my source work by inserting an if statement but then I get cpmbinations that repeat...
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<fstream>
#include<iostream>
#include<cstring>

using namespace std;




void char_permutation(char str[],char append[] )

	{
	ofstream myfile;
myfile.open("Ultimate combinations.txt", ios::out | ios::app);
	
		int length = strlen(str);
		
		if (length)
			{
				for(int i=0;i<length;++i)
				{
				char* str1 = new char[length+1];
				int cnt;
				int cnt2;
				for(cnt=0,cnt2=0; cnt<length; ++cnt,++cnt2)
				{
				if (cnt == i)
				{
				str1[cnt] = str[++cnt2];
				continue;
				} 
			
			        	else
					str1[cnt] = str[cnt2];
					}

					str1[cnt] = '\0';
					int alength = strlen(append);
					
				
					if (alength>2 && i==0)
						{
						myfile << append << endl;
					}
				
					char* append1 = new char [alength+2];
					strncpy(append1,append,alength);
					append1[alength] = str[i];
					append1[alength+1] = '\0';
					char_permutation(str1,append1);
					delete []str1;
					delete []append1;
					}
			}
		else
		{
		myfile << append << endl;
		}
}


int main()
{
  
char str[80]; 
char append[] = "\0";


cin >> str;

cout << "Original = " << str << endl;
char_permutation(str,append);
cout << "Done ........" << endl;


system("PAUSE");
return 0;
}
Topic archived. No new replies allowed.