### Replacing Permutations within a string I have a problem. I have a string of determined length that includes 1s, 0s and ?s. For example, my string is 001110101?1?110?. From this string, I have to calculate the number of ?s and replace them with all permutations of 0s and 1s. So all ?s become all 1s, all 0s or all the possible permutations of 0s and 1s in between. I have to display each permutation onto my console.

How do I calculate each set of permutations? And how/where do I store them?

Here's what I have so far. All my code does at this point is keep track of the index at which the ? occurs.

#include <iostream>
using namespace std;

int main()
{

string s = "10101?1?110?";
int count = {0};
int j = 0;

for (int i = 0; i < s.size(); i++)
{
if (s[i] == '?')
{
count[j] = i;
j++;
cout << i << endl;
}
}

for (int l = 0; l < 12; l++)
{
cout << count[l];
}
} Binary counting will give you the combinations. e.g.

00000000
00000001
00000010
00000011
00000100
.
.
.
11111110
11111111

This cycles through all combinations of 0's and 1's and can be achieved using an ordinary unsigned int (you can use a 32 bit unsigned int to get the combinations for up to 32 positions)

For each value you need to extract the 0's and 1's and you can do this using the bitshift operator >> as well as the bitwise logical operator &

e.g. if x is the unsigned int and you want the nth binary digit

digit = (x>>n) & 0x1

Topic archived. No new replies allowed.