fork(2) download
  1. #include <stdio.h>
  2.  
  3. // Find all binary strings that can be formed from given
  4. // wildcard pattern
  5. void printAllCombinations(char pattern[], int i)
  6. {
  7. if (pattern[i] == '\0')
  8. {
  9. printf("%s\n", pattern);
  10. return;
  11. }
  12.  
  13. // if the current character is '?'
  14. if (pattern[i] == '?')
  15. {
  16. for (int k = 0; k < 2; k++)
  17. {
  18. // replace '?' with 0 and 1
  19. pattern[i] = k + '0';
  20.  
  21. // recuse for the remaining pattern
  22. printAllCombinations(pattern, i + 1);
  23.  
  24. // backtrack (As array is passed by reference to the function)
  25. // pattern[i] = '?';
  26. }
  27. return;
  28. }
  29.  
  30. // if the current character is 0 or 1, ignore it and
  31. // recuse for the remaining pattern
  32. printAllCombinations(pattern, i + 1);
  33. }
  34.  
  35. int main()
  36. {
  37. char pattern[] = "1?11?00?1?";
  38.  
  39. printAllCombinations(pattern, 0);
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
1011000010
1011000011
1011000111
1011100111
1111100111