[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Scrambling strings in C++

Posted on 2006-04-26
5
Medium Priority
?
739 Views
Last Modified: 2010-10-05
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
0
Comment
Question by:Linky
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 16545651
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;
}
0
 
LVL 5

Assisted Solution

by:B1-66ER
B1-66ER earned 80 total points
ID: 16546529
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;
  }
0
 
LVL 86

Expert Comment

by:jkr
ID: 16546555
>>And to get another part of permutations you need to use prev_permutation

Why? That loop performs all possible permutations.
0
 
LVL 2

Author Comment

by:Linky
ID: 16547144
Thanks for the help guys. I knew STL had something to do it.
0
 
LVL 5

Expert Comment

by:B1-66ER
ID: 16547368
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;
  }
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question