I am trying to figure out how to generate a word list in C++ based on a string of characters given (similar to how crunch wordlist works)
I am fairly new to C++ and have only gotten the program to list the characters off one at a time.
I've been searching online for quite some time with not much luck other than finding O(n^2) with not much idea on how to implement it to the program.
Code:
int main() {
string characters = "abcde";
int length = 5;
string word = "";
for(int i = word.length(); i <= length; i++) {
for(int l = 0; l < characters.length(); l++) {
word += characters[l];
cout << word << "\n";
}
}
return 0;
}
Result:
a
b
c
d
e
Wanted Result: http://pastebin.com/tgyUtKfA
Snippet of result:
a
b
c
d
e
aa
ab
ac
ad
ae
ba
bb
bc
bd
be
ca
cb
cc
cd
ce
da
db
dc
dd
de
ea
eb
ec
ed
ee
aaa
(The final result follows that pattern down to "eeeee")
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<string> make_permutations(int length) {
if (length == 0) {
return vector<string>{};
}
vector<string> results;
vector<string> children = make_permutations(length - 1);
for (char letter = 'a'; letter <= 'z'; letter++) {
results.emplace_back(1, letter);
for(auto child : children) {
results.push_back(std::string(1, letter) + child);
}
}
return results;
}
int main()
{
auto results = make_permutations(2);
for(auto s : results) cout << s << endl;
}