Skip to content

Instantly share code, notes, and snippets.

@pierceh89
Created May 1, 2016 17:29
Show Gist options
  • Save pierceh89/7740bfd5787569066bed92ce1eb34957 to your computer and use it in GitHub Desktop.
Save pierceh89/7740bfd5787569066bed92ce1eb34957 to your computer and use it in GitHub Desktop.
This prints out all possible combination of the given string.
// print all combinations of string
#include <iostream>
#include <vector>
using namespace std;
void combinations(const char str[]);
void combinations(const char str[], char buf[], bool used[], int bufpos, int cur, int len);
void combinations(const char str[], vector<char>& buf, int start, int len);
int main(int argc, char* argv[])
{
if(argc!=2)
{
cerr << argv[0] << " [string]" <<endl;
return -1;
}
combinations(argv[1]);
return 0;
}
void combinations(const char str[], vector<char>& buf, int start, int len)
{
for(int i=start; i<len; ++i)
{
buf.push_back(str[i]);
//cout <<"bufsize: " << buf.size() << endl;
for(size_t j=0; j<buf.size(); ++j)
cout << buf[j];
cout << endl;
if( i < len)
combinations(str, buf, i+1, len);
buf.resize(buf.size()-1);
}
}
void combinations(const char str[])
{
int len = strlen(str);
vector<char> buf;
combinations(str, buf, 0, len);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment