Skip to content

Instantly share code, notes, and snippets.

@dalslandan200
Last active March 6, 2020 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalslandan200/c4165203151ce99717f6e83374fd3a4a to your computer and use it in GitHub Desktop.
Save dalslandan200/c4165203151ce99717f6e83374fd3a4a to your computer and use it in GitHub Desktop.
C++ iterate all combinations from string(char[]). Only ASCII is supported.
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
/**
* I could not find anywhere a simple function, which iterates a set of ASCII characters,
* and count them in sequence like various numeric systems (base10, base16, etc.).
*
* In short it tests all possible combinations. While doing it in a numeric counting fashion.
* I hope it helped you in some way, you are free to modify it and use however you wish without attribution!
*
*/
// Find and return the position in the passed SET.
// INFO: Special helper function to stringSequence.
size_t findPositionSet(const char &needle, const std::string &haystack) {
// Store position
size_t position = 0;
// Iterate haystack
for (size_t i = 0; i < haystack.length(); i++) {
// Check match
if (haystack[i] == needle) {
position = i;
break;
}
// Check if last iteration
if (i == haystack.length() - 1) {
std::cout << "ERROR: Tools::findPositionSet needle not found in haystack" << std::endl;
exit(-1);
}
}
return position;
}
// Special function, test all scenarios std::string
// INFO: Pass old string, to return the next string in order.
std::string stringSequence(const std::string &old) {
// Possible outcomes for all chars
//const static std::string SCENARIOS = "0123456789@abcdefghijklmnopqrstuvwxyz";
// IMPORTANT: When adding ASCII chars to the string, they must have the same order from ASCII table.
// (If not respected, comparison will fail and produce inaccurate results.)
const static std::string SCENARIOS = "0123";
// Used for new string
std::stringstream buffer;
// Error checking
if (old.length() < 1) {
std::cout << "ERROR: Tools::stringSequence() passed string invalid length" << std::endl;
exit(-1);
}
// Used for detecting increment position.
unsigned int incrementPosition;
bool incrementPosition_found = false;
// Find out which position to increment
for (int i = old.length() - 1; i >= 0; i--) {
// If current char, is not last in SCENARIOS
if (old[i] < SCENARIOS[SCENARIOS.length() - 1])
{
incrementPosition = i;
incrementPosition_found = true;
break;
}
}
// Form the new string
if (incrementPosition_found) {
// Iterate up to incrementPosition
for (int i = 0; i < incrementPosition; i++) {
// Append old char
buffer << old[i];
}
// Append new char
buffer << SCENARIOS[findPositionSet(old[incrementPosition], SCENARIOS) + 1];
// Iterate remaining part of old string.
for (int i = incrementPosition + 1; i < old.length(); i++) {
// Append lowest value of SCENARIOS
buffer << SCENARIOS[0];
}
}
else { // All char(s) are the last char of SCENARIOS
// Append first char of SCENARIOS
buffer << SCENARIOS[0];
// Iterate remaining length of old string.
for (int i = 0; i < old.length(); i++) {
buffer << SCENARIOS[0];
}
}
return buffer.str();
}
int main() {
// Start position
std::string start = "0";
// Iterate
for (int i = 0; i < 32; i++)
{
std::cout << start << std::endl;
// Passing the old variable, to get the next string in order.
start = stringSequence(start);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment