Find Subsets of Given String
Method I:Can find substring for any length of string because we are taking binary number as string while Conversion itself
Method II:Can find subsets for smaller strings as we are taking binary number as integer
Example for Subsets of string Tea is
000-Nothing is printed
001-a
010-e
011-ea
100-T
101-Ta
110-Te
111-Tea
So its nothing but
T,
e
a
Te
Ta
ea
Tea
Similarly Help
H
e
l
p
He
Hl
Hp
el
ep
lp
Hel
Hep
Hlp
elp
Help
//Coming Soon...
Output:
Method I:Can find substring for any length of string because we are taking binary number as string while Conversion itself
#include<stdio.h>
#include<string.h>
#include<math.h>
char *GetBinary(int dec);
int main()
{
int i,j,len,count=0,noofzeros;
char str[20],temp[20],binary[20];
printf("Enter a string\n");
gets(str);
len=strlen(str);
printf("Subsets of Given String are");
for(i=0;i<(pow(2,len));i++)//2^len is the number of subsets for given length.
{
strcpy(binary,GetBinary(i));
noofzeros=len-strlen(binary);//Since zeros will not be there before single digits and double etc depending on length of binary number.
//To compensate them we used here
for(j=0;j<len;j++)
{
if(j>=noofzeros)
{
if(binary[j-noofzeros]!='0')
printf("%c",str[j]);
}
}
printf("\n");
}
return(0);
}
char *GetBinary(int dec)
{
int temp,i,j=1,k=0;
char binary[20];
temp=dec;
while(temp!=0)
{
i=temp%2;
binary[k]=i+'0';
temp=temp/2;
j=j*10;
k++;
}
binary[k]='\0';
return(strrev(binary));
}
Method II:Can find subsets for smaller strings as we are taking binary number as integer
#include<stdio.h>
#include<string.h>
#include<math.h>
int GetBinary(int dec);
main()
{
int i,j,binary,len,count=0,tempLen;
char str[20],temp[20];
printf("Enter a string\n");
gets(str);
len=strlen(str);
for(i=1;i<(pow(2,len));i++)//2^len is the number of subsets for given length
{
binary=GetBinary(i);
sprintf(temp, "%d", binary);//To convert integer to string
tempLen=len-strlen(temp);
for(j=0;j<len;j++)
{
if(j>=tempLen)
{
if(temp[j-tempLen]!='0')
printf("%c",str[j]);
}
}
printf("\n");
}
}
int GetBinary(int dec)
{
int temp,i,j=1,binary=0;
temp=dec;
while(temp!=0)
{
i=temp%2;
binary=binary+(i*j);
temp=temp/2;
j=j*10;
}
return(binary);
}
Explanation:000-Nothing is printed
001-a
010-e
011-ea
100-T
101-Ta
110-Te
111-Tea
So its nothing but
T,
e
a
Te
Ta
ea
Tea
Similarly Help
H
e
l
p
He
Hl
Hp
el
ep
lp
Hel
Hep
Hlp
elp
Help
Output:


