The STL has a function next_permutation() which you can apply to a vector. This sample code is written for integers, it is your mission to apply your genius to make it work with characters! Report back your success to this illustrious forum!
// permutations of a three digit integer number
// Dev C++
#include <iostream>
#include <algorithm> // next_permutation() via stl_algo.h
#include <vector> // stl vector header
#include <iterator> // ostream_iterator
#include <stdlib.h> // system()
using namespace std;
int main()
{
vector<int> iV;
iV.push_back(0);
iV.push_back(1);
iV.push_back(2);
// display original
cout << "original number:\n";
copy(iV.begin(),iV.end(),ostream_iterator<int>(cout,""));
cout << endl;
cout << "permutations:\n";
// loop until all permutations are generated and displayed
// does not display original number
while (next_permutation(iV.begin(), iV.end()))
{
copy(iV.begin(),iV.end(),ostream_iterator<int>(cout,""));
cout << endl;
}
system("PAUSE");
return 0;
}
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Any reasonably modern C++ compiler should give you access to the Standard Template Libraries!!!
If Borland Turbo C++ does not provide STL, you do yourself a favor by getting into a more updated version. You can always download the Dev C++ IDE, which uses the open source GCC/G++ compiler, for free at:
http://sourceforge.net/projects/dev-cpp/
I recommend it, it installs easily and it's a joy to code and compile with this system.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Actually, the algorithm for the next_permutation() is in the header file called stl_algo.h
Once you installed DevCpp this header file should be in
..\include\C++\3.3.1\bits\
Other compilers may have different directories.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
I guess we can all google! Here is in interesting way for character permutations. Check the permutation after the original.
// This program finds permutations using a recursive method
// modified Dev C++ from a wonderful article at:
// http://www.codeproject.com/cpp/cppperm1.asp
#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];
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;
}
There is also code in the C code snippets section on DaniWeb. Read Dave's comments first, bottom of snippet.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
You can do it your self if you know some math :)
w o r d = 4 letters which aren't the same
= 4!
s h e e p = 5! / 2! (the 2 ee)
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
Sounds like another fail, I dare people not to help you.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Sounds like another fail, I dare people not to help you.
Why on earth did you dig up this thread to post such a reply? Youdo realize that it's quite save to assume no-one was going to reply after a year of silence right?
There is loophole in this program.
whom so ever who has created it, so smartly
have forgotten to do something very important in this program.
shit huge blunder
wanna know then ask me
Why won't you just post it here? No-one is going to email you for this problem and risk getting spam in return :icon_wink:
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403