View difference between Paste ID: Tu5F3Kzz and sQBghMdZ
SHOW: | | - or go back to the newest paste.
1
/* Program to generate all r-Combinations of a set with distinct element
2
 * This code is a part of https://phoxis.wordpress.com/2009/10/13/allcombgen/
3
 */
4
 
5
#include <stdio.h>
6
#include <string.h>
7
#include <stdlib.h>
8
 
9
#define MAX 100
10
#define TRUE 1
11
#define FALSE 0
12
 
13
int generate_combination (const char *source_string,char *combination_string);
14
 
15
unsigned int comb_mask = 0x00000001;
16
unsigned int max_mask_length;
17
 
18
/* Main Function, driving generate_combination() */
19
int main (void)
20
{
21
  char combination_string[MAX];
22
  int len, group_len;
23
24
  char *source_string ="abcdefghijklmnopqrstuvwxyz";
25-
  group_len = 4;
25+
  group_len = 10;
26
 
27
  //printf ("Enter the Source String:");
28
  //scanf ("%s", source_string);
29
 
30
  //printf ("Enter the Group Length:");
31
  //scanf ("%d", &group_len);
32
33
  len = strlen (source_string);
34
  max_mask_length = ~(0x00000001 << len);
35
 
36
  //printf ("\nPrinting Combinatins:\n");
37
  while (1)
38
    {
39
      if (generate_combination (source_string, combination_string) == FALSE)
40
        break;
41
      
42
      if(strlen(combination_string)==group_len) printf ("%s\n", combination_string);
43
    }
44
  //printf ("\nfinished\n");
45
  return 0;
46
}
47
 
48
/*
49
 *  File Name     : combination.c
50
 *  Function Name : generate_combination
51
 *  Parameters    :
52
 *                @ (const char *) source_string
53
 *                @ (char *)       combination_string
54
 *  Return Type   : (int)
55
 *                    # return FALSE if no more mermutations, else return TRUE
56
 *  Globals       :
57
 *                @ (unsigned int) comb_mask
58
 *                    # A bitmask. Used to select unique combinations from source
59
 *                      string. This function increments and updates this mask
60
 *                      in each iteration. Each call results in a new permutation.
61
 *                @ (unsigned int) max_mask_length
62
 *                    # A bitmask. Used to control the length of masking of
63
 *                      comb_mask variable. This is not modified in this function.
64
 *                      Initilized in function main()
65
 *  Description   : Each call to this function generated a new combination from
66
 *                  the source_string and then places into combination_string
67
 *                  The combination is done based upon comb_mask variable. The
68
 *                  positions, in which comb_mask has a '1' , are only selected
69
 *                  from the source_string to make a combination.
70
 *  Note          : The combination generation is comb_mask dependent. To generate
71
 *                  unique combinations in each call comb_mask should not be modified
72
 *                  any where else, if some explicit customized combinination is to be
73
 *                  generated. Each bit field has a fixed length (32bit) , the combintaion
74
 *                  can be generated is limited to this length, in this version of code.
75
 *                  This function does not return the null set.
76
 */
77
 
78
int generate_combination (const char *source_string, char *combination_string)
79
{
80
  unsigned int mask = 0x00000001;
81
  int s_pos = 0, c_pos = 0;
82
 
83
  /* Main logic */
84
  while ((mask & max_mask_length))
85
    {
86
      if ((comb_mask & mask))
87
      {
88
          combination_string[c_pos] = source_string[s_pos];
89
          c_pos++;
90
      }
91
      s_pos++;
92
      mask <<= 1;
93
    }
94
 
95
  /*update permutation mask */
96
  comb_mask++;
97
 
98
  /* Terminate the combination_string with NULL character */
99
  combination_string[c_pos] = 0;
100
 
101
  /* If combination_string is empty, ie. c_pos == 0 , return FALSE else return TRUE */
102
  return (c_pos == 0 ? FALSE : TRUE);
103
}