It's me again!! Can someone please help me with my C++ code?
I need to write a permutation method which is iterative. Now, I am getting some output, but it is repeating a bit. Could someone please tell me where I am going wrong?
The permutation Method:
Code: Select all
void permuteIterate(string* c,stack<string>* s)
{
int length = c->length();
for (int i = 0; i < length; i++)
{
for (int k = 0; k < length; k++)
{
if (k != i)
{
swapChars(c,i,k);
s->push(*c);
}
}
}
}
Code: Select all
void swapChars(string* s, int j, int i)
{
char tmp = (*s)[i];
(*s)[i] = (*s)[j];
(*s)[j] = tmp;
}
Code: Select all
Enter String:
abc
The iterative version returns:
cab
cba
abc
acb
cab
bac
Thanks for the help!