Print every string possible when each char can be two options

Hello, I need to make a program that will allow me to print every possible string when every .at can be two possibilities. So I will need to have some type of system to reflect this variance, but I don't know where to start. The longest string will be 8 elements long. How can I decide which variables can be picked?
> So I will need to have some type of system to reflect this variance,

Since a bit can take one of two possible values, one way is to represent each string as an n-bit integer (where n is the length of the string).

Another way is recursive: to generate a string of length n, prefix (or suffix) each of the two characters to every generated string of length n-1.

Spoiler: http://coliru.stacked-crooked.com/a/0ea07ea4b9c3d7e4
I have it working pretty much. I just need to be able to copy the output to a clipboard and CTRL+C closes my terminal every time. I am confused on how to print out to a file. Can someone please tell me what I am doing wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

std::string make_string(unsigned int value, std::size_t nchars, char a, char b)
{
	std::string result;
	for (; result.size() < nchars; value /= 2) result += value & 1U ? a : b;
	if (result.at(0) == '0')
	{
		result.at(0) = 'p';
	}
	else
	{
		result.at(0) = 'p';
	}
	if (result.at(1) == '0')
	{
		result.at(1) = '5';
	}
	else
	{
		result.at(1) = 'M';
	}
	if (result.at(2) == '0')
	{
		result.at(2) = '8';
	}
	else
	{
		result.at(2) = 'x';
	}
	if (result.at(3) == '0')
	{
		result.at(3) = '3';
	}
	else
	{
		result.at(3) = 'J';
	}
	if (result.at(4) == '0')
	{
		result.at(4) = 'N';
	}
	else
	{
		result.at(4) = 'Q';
	}
	if (result.at(5) == '0')
	{
		result.at(5) = 'e';
	}
	else
	{
		result.at(5) = '9';
	}
	if (result.at(6) == '0')
	{
		result.at(6) = 'o';
	}
	else
	{
		result.at(6) = '7';
	}
	if (result.at(7) == '0')
	{
		result.at(7) = 'I';
	}
	else
	{
		result.at(7) = 'm';
	}
	return result; // note: the bits are in reverse
}

void print_strings(std::size_t nchars, char a, char b)
{
	std::ofstream myfile("example.txt");
	myfile.open("example.txt");
	const unsigned int ub = 1U << nchars;
	for (unsigned int n = 0; n < ub; ++n)
	{
		myfile << std::setw(3) << make_string(n, nchars, a, b) << '\n';
	}
}

int print_strings_alt(std::size_t nchars, char a, char b, std::string str = {}, int n = 0)
{
	if (nchars == 0)
	{
		if (str.at(0) == '0')
		{
			str.at(0) = 'p';
		}
		else
		{
			str.at(0) = 'p';
		}
		if (str.at(1) == '0')
		{
			str.at(1) = '5';
		}
		else
		{
			str.at(1) = 'M';
		}
		if (str.at(2) == '0')
		{
			str.at(2) = '8';
		}
		else
		{
			str.at(2) = 'x';
		}
		if (str.at(3) == '0')
		{
			str.at(3) = '3';
		}
		else
		{
			str.at(3) = 'J';
		}
		if (str.at(4) == '0')
		{
			str.at(4) = 'N';
		}
		else
		{
			str.at(4) = 'Q';
		}
		if (str.at(5) == '0')
		{
			str.at(5) = 'e';
		}
		else
		{
			str.at(5) = '9';
		}
		if (str.at(6) == '0')
		{
			str.at(6) = 'o';
		}
		else
		{
			str.at(6) = '7';
		}
		if (str.at(7) == '0')
		{
			str.at(7) = 'I';
		}
		else
		{
			str.at(7) = 'm';
		}
		std::ofstream myfile("example.txt");
		myfile.open("example.txt");
		myfile << std::setw(3) << str << '\n';
		return n + 1;
	}
	else
	{
		const int nxt = print_strings_alt(nchars - 1, a, b, str + b, n);
		return print_strings_alt(nchars - 1, a, b, str + a, nxt);
	}
}

int main()
{
	std::ofstream myfile("example.txt");
	myfile.open("example.txt");

	print_strings(8, '0', '1');
	std::cout << "-------------------\n";
	print_strings_alt(8, '0', '1');
	myfile.close();
}
I just need to be able to copy the output to a clipboard and CTRL+C closes my terminal every time.

Try this key sequence instead of CTRL-C
ALT-SPACE
E
S
ENTER
awesome! tyvm!
So the purpose of this was to calculate every single possible string that I can have from a cipher paper that I use to make passwords. It's just a line going through chars and ints that I printed out on a paper that covers one whole side of the paper and I received the digits from the perfect password website. I mistakenly made bad lines due to tiredness and was changing out the passwords. Fortunately my external didn't brick lol.... I did 350 entries... Such tedium... Macro buttons on mouses help a lot. Is anyone aware of any other methods?
Avoiding hard-coded values and the long sequence if-else constructs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>

std::string make_string( unsigned int value, const std::string& a, const std::string& b )
{
    const std::size_t nchars = std::min( a.size(), b.size() ) ;
    std::string result ;

    for( std::size_t i = 0 ; i < nchars ; ++i )
    {
        result += value&1U ? a[i] : b[i] ;
        value /= 2 ;
    }

    return result ; // note: the bits are in reverse
}

std::ostream& print_strings( const std::string& a, const std::string& b, std::ostream& stm = std::cout )
{
    const unsigned int ub = 1U << std::min( a.size(), b.size() ) ;

    for( unsigned int n = 0 ; n < ub ; ++n ) stm << make_string( n, a, b ) << '\n' ;

    return stm ;
}

int main()
{
    const std::string first = "ABCDEFGH" ;
    const std::string second = "STUVWXYZ" ;

    print_strings( first, second ) ; // print to stdout

    std::ofstream myfile( "example.txt" ) ;
    print_strings( first, second, myfile ) ; // print to file
}
Topic archived. No new replies allowed.