| | |
Permutation
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hello once again . My teacher has given us the assignment to enter a string a maximum of 7 charecters and permute it.
Then print all of possible permutations . Like if the inputted word is
JAY so Output would be
JAY
AJY
AYJ
JYA
YAJ
YJA
I do not seem to understand how to go about it. Please help.
I wish to know about some initial logic before developing the right code.
Thank you in advance.
hinduengg
Then print all of possible permutations . Like if the inputted word is
JAY so Output would be
JAY
AJY
AYJ
JYA
YAJ
YJA
I do not seem to understand how to go about it. Please help.
I wish to know about some initial logic before developing the right code.
Thank you in advance.
hinduengg
Last edited by hinduengg; Jul 6th, 2007 at 3:02 pm.
Learning C++ is easy with Daniweb :)
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
Since this is probably a learning experience in how to manipulate data rather than scrounging the archives of the Standard Template Library, I would suggest you consider this:
Using the JAY example if you did this:
JAY
JYA
AJY
AYJ
YJA
YAJ
and note that you can get all permutations by taking each element of the original and combining it with each of the remaining elements in order. Each element could be an element of an array and then you could loop through the array starting each loop with a new element and using other loops to get the rest of the elements in order. This process lends itself quite nicely to the concept of nested loops, which I have little doubt was the subject of one of your classes, although as iamthwee indicates there are other ways of doing this.
Using the JAY example if you did this:
JAY
JYA
AJY
AYJ
YJA
YAJ
and note that you can get all permutations by taking each element of the original and combining it with each of the remaining elements in order. Each element could be an element of an array and then you could loop through the array starting each loop with a new element and using other loops to get the rest of the elements in order. This process lends itself quite nicely to the concept of nested loops, which I have little doubt was the subject of one of your classes, although as iamthwee indicates there are other ways of doing this.
Last edited by Lerner; Jul 6th, 2007 at 3:13 pm.
•
•
•
•
if you show us how far you have gone, it will be easier for us to help you
this is so far I have developed the code:
C++ Syntax (Toggle Plain Text)
#include<iostream> int main() { int c,y,m=0; int z,fact; char a[100]; for(y=0;a[y]!='\0';y++) c++; cout<<"the length"<<c<<endl; for(x=1;x<=c;x++) { fact=1; fact=fact*c; } cout<<"no of permutations"<<fact<<endl; for(int w=1;w<=fact;w++) for(int y=0;a[y]!='\0';y++) { m=y+1; cout<<a[m]; }
I am unable to understand what to do to print permutations
and address index of the previous value of the string.
Last edited by ~s.o.s~; Jul 7th, 2007 at 10:58 am. Reason: Fixed code tags, learn to use them.
Learning C++ is easy with Daniweb :)
If you know what a flow chart is you better start drawing one.
Address the parts in red...
#include <iostream>
int main()
{
int c, y, m = 0;
int z, fact;
char a[100];
for ( y = 0; a[y] != '\0'; y++ )
c++;
cout << "the length" << c << endl;
for ( x = 1; x <= c; x++ )
{
fact = 1;
fact = fact * c;
}
cout << "no of permutations" << fact << endl;
for ( int w = 1; w <= fact; w++ )
for ( int y = 0; a[y] != '\0'; y++ )
{
m = y + 1;
cout << a[m];
}
}
Address the parts in red...
Last edited by iamthwee; Jul 7th, 2007 at 10:02 am.
*Voted best profile in the world*
this is the code that I actually got while browsing through code snippets
Please devote some of your precious time in making me understand this.
My question is in the code comments , please,please tell the reason for that. Varunthai had understood it but he did not write any grain of logic for me to understand!
Till now I have understood that there are 2 strings on which permutation is working but the process of cnt cnt2 ,and statements like - char* append1 = new char [alength+2]; confuse me then.
please help
I would appreciate your help greatly
hinduengg
C++ Syntax (Toggle Plain Text)
#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; } else str1[cnt] = str[cnt2]; } str1[cnt] = '\0'; int alength = strlen(append); char* append1 = new char [alength+2];// why adding 2 here strncpy(append1,append,alength); append1[alength] = str[i]; append1[alength+1] = '\0'; char_permutation(str1,append1); delete []str1; delete []append1; } } else { cout << append << endl; } } int main() { char str[] = "BUSH"; // shows a little humor char append[] = "\0"; cout << "Original = " << str << endl; char_permutation(str,append); cout << "Done ........" << endl; cin.get(); // wait return 0; }
Please devote some of your precious time in making me understand this.
My question is in the code comments , please,please tell the reason for that. Varunthai had understood it but he did not write any grain of logic for me to understand!
Till now I have understood that there are 2 strings on which permutation is working but the process of cnt cnt2 ,and statements like - char* append1 = new char [alength+2]; confuse me then.
please help

I would appreciate your help greatly
hinduengg
Last edited by WaltP; Jul 8th, 2007 at 1:29 am. Reason: Fixed CODE tags -- please use the PREVIEW button
Learning C++ is easy with Daniweb :)
•
•
Join Date: Jun 2007
Posts: 13
Reputation:
Solved Threads: 1
The idea here is to make each character in the string exist in every position, for example:
string is "JOY"
so taking J:-
1st pass: "JOY"
2nd pass: "OJY"
3rd pass: "OYJ"
taking O:-
1st pass: "OJY"
2nd pass: "JOY"
3rd pass: "JYO"
taking Y:-
1st pass: "YOJ"
2nd pass: "JYO"
3rd pass: "JOY"
then remove the repeated results, and you'll get all the combinations for that string
string is "JOY"
so taking J:-
1st pass: "JOY"
2nd pass: "OJY"
3rd pass: "OYJ"
taking O:-
1st pass: "OJY"
2nd pass: "JOY"
3rd pass: "JYO"
taking Y:-
1st pass: "YOJ"
2nd pass: "JYO"
3rd pass: "JOY"
then remove the repeated results, and you'll get all the combinations for that string
![]() |
Similar Threads
- permutation of a string in c++ (C++)
- Prolog permutation (Legacy and Other Languages)
Other Threads in the C++ Forum
- Previous Thread: Inverting 24-bit bitmap using fstream c++
- Next Thread: class type
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






