C Program to Find All Possible Subsets of a String

This C Program finds possible subsets of the String.

Here is source code of the C Program to find the possible subsets of the String. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to find the possible subsets of the String
  3.  */
  4. #include <stdio.h>
  5.  
  6. char string[50], n;
  7. void subset(int, int, int);
  8.  
  9. int main()
  10. {
  11.     int i, len;
  12.  
  13.     printf("Enter the len of main set : ");
  14.     scanf("%d", &len);
  15.     printf("Enter the elements of main set : ");
  16.     scanf("%s", string);
  17.     n = len;
  18.     printf("The subsets are :\n");
  19.     for (i = 1;i <= n;i++)
  20.         subset(0, 0, i);
  21. }
  22.  
  23. /*Function to find the number of subsets in the given string*/
  24.  
  25. void subset(int start, int index, int num_sub)
  26. {
  27.     int i, j;
  28.     if (index - start + 1  ==  num_sub)
  29.     {
  30.         if (num_sub  ==  1)
  31.         {
  32.             for (i = 0;i < n;i++)
  33.                 printf("%c\n", string[i]);
  34.         }
  35.         else
  36.         {
  37.             for (j = index;j < n;j++)
  38.             {
  39.                 for (i = start;i < index;i++)
  40.                     printf("%c", string[i]);
  41.                 printf("%c\n", string[j]);
  42.             }
  43.             if (start != n - num_sub)
  44.                 subset(start + 1, start + 1, num_sub);
  45.         }
  46.     }
  47.     else
  48.     {
  49.         subset(start, index + 1, num_sub);
  50.     }
  51. }

$ cc string19.c
$ a.out
Enter the len of main set : 11
Enter the elements of main set : programming
The subsets are :
p
r
o
g
r
a
m
m
i
n
g
pr
po
pg
pr
pa
pm
pm
pi
pn
pg
ro
rg
rr
ra
rm
rm
ri
rn
rg
og
or
oa
om
om
oi
on
og
gr
ga
gm
gm
gi
gn
gg
ra
rm
rm
ri
rn
rg
am
am
ai
an
ag
mm
mi
mn
mg
mi
mn
mg
in
ig
ng
pro
prg
prr
pra
prm
prm
pri
prn
prg
rog
ror
roa
rom
rom
roi
ron
rog
ogr
oga
ogm
ogm
ogi
ogn
ogg
gra
grm
grm
gri
grn
grg
ram
ram
rai
ran
rag
amm
ami
amn
amg
mmi
mmn
mmg
min
mig
ing
prog
pror
proa
prom
prom
proi
pron
prog
rogr
roga
rogm
rogm
rogi
rogn
rogg
ogra
ogrm
ogrm
ogri
ogrn
ogrg
gram
gram
grai
gran
grag
ramm
rami
ramn
ramg
ammi
ammn
ammg
mmin
mmig
ming
progr
proga
progm
progm
progi
progn
progg
rogra
rogrm
rogrm
rogri
rogrn
rogrg
ogram
ogram
ograi
ogran
ograg
gramm
grami
gramn
gramg
rammi
rammn
rammg
ammin
ammig
mming
progra
progrm
progrm
progri
progrn
progrg
rogram
rogram
rograi
rogran
rograg
ogramm
ogrami
ogramn
ogramg
grammi
grammn
grammg
rammin
rammig
amming
program
program
prograi
progran
prograg
rogramm
rogrami
rogramn
rogramg
ogrammi
ogrammn
ogrammg
grammin
grammig
ramming
programm
programi
programn
programg
rogrammi
rogrammn
rogrammg
ogrammin
ogrammig
gramming
programmi
programmn
programmg
rogrammin
rogrammig
ogramming
programmin
programmig
rogramming
programming

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.