Scrambling strings in C++

Does anyone have some code that takes a string (stl string class), and prints out every single combinations of characters for that string? I'm looking for a brute-form algorithm that takes a string and prints out every possible scrambling for it. I need it for a game I am programming in my free time. Code would be nice, thank you.

Like "cat" would print:

atc
tac
tca
....
etc
LVL 2
LinkyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Sure, use 'next_permutation', e.g.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std ;

int main ()
{

string s = "cat";

    while ( next_permutation(s.begin(), s.end()) )
    {
        cout << s << endl;
    }

return 0;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
B1-66ERCommented:
Hi Linky.
And to get another part of permutations you need to use prev_permutation (add next lines in jkr code):

  s = "cat";
  while ( prev_permutation(s.begin(), s.end()) )
  {
    cout << s << endl;
  }
jkrCommented:
>>And to get another part of permutations you need to use prev_permutation

Why? That loop performs all possible permutations.
LinkyAuthor Commented:
Thanks for the help guys. I knew STL had something to do it.
B1-66ERCommented:
Hi jkr.
>> Why? That loop performs all possible permutations.

The loop performs all possible permutations if you pass all symbols in alphabet order ( instead "cat" must be "act"). But if we will pass "cat", then to get all possible permutations, we must make permutation in two ways: next and prev.

So Linky can use only your example but instead "cat" write "act"

string s = "act";

  while ( next_permutation(s.begin(), s.end()) )
  {
    cout << s << endl;
  }
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.