We are going to share a C program to find the possible subsets of the string. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to find the possible subsets of the string.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
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 | #include <stdio.h> char string[50], n; void subset(int, int, int); int main() { int i, len; printf("Enter the len of main set : "); scanf("%d", &len); printf("Enter the elements of main set : "); scanf("%s", string); n = len; printf("The subsets are :\n"); for (i = 1;i <= n;i++) subset(0, 0, i); } /*Function to find the number of subsets in the given string*/ void subset(int start, int index, int num_sub) { int i, j; if (index - start + 1 == num_sub) { if (num_sub == 1) { for (i = 0;i < n;i++) printf("%c\n", string[i]); } else { for (j = index;j < n;j++) { for (i = start;i < index;i++) printf("%c", string[i]); printf("%c\n", string[j]); } if (start != n - num_sub) subset(start + 1, start + 1, num_sub); } } else { subset(start, index + 1, num_sub); } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.