Created
May 1, 2016 17:29
-
-
Save pierceh89/7740bfd5787569066bed92ce1eb34957 to your computer and use it in GitHub Desktop.
This prints out all possible combination of the given string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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