How do I write a program to print proper subset of given
string . Eg :input: abc
output:{},{a},{b},{c},{a,b},{a,c},{b,c},
{a,b,c}.I desperately need this program please mail me to
saravana6m@gmail.com

Answer Posted / suchitpuri

i have written a program in java which print all the subsets
i am using binary representation to calculate the subset
ie 100 means a
101 means ac
111 abc and so on


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author spuri
*/
public class printSubsets
{

static char input[] = { 'a', 'b', 'c' };

/**
* @param args
* the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here


Integer i = new Integer((int) Math.pow(2, input.length));

for (int k = 0; k < i; k++)
{
if (k == 0)
{
System.out.print("{}");
}
else
{
String temp = new String();
char op[] = Integer.toBinaryString(k).toCharArray();
if (op.length <= input.length)
{
for (int j = 0; j < (input.length - op.length); j++)
{
temp = temp + "0";
}
}
temp = temp + new String(op);
printSequence(temp.toCharArray());

}

}

}

public static void printSequence(char[] op)
{

System.out.print("{");

for (int i = 0; i < op.length; i++)
{

if (op[i] == '1')
{
System.out.print(input[i]);

}
}
System.out.print("}");

}
}

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to palindrom string in c language?

8929


Sir... please give some important coding questions asked by product companies..

1813


How can I Create a C program in splitting set of characters to specific subsets. Example: INPUT SET OF CHARACTERS: Therefore, my dear brothers and sisters, stand firm. Let nothing move you. Always give yourselves fully to the work of the Lord, because you know that your labor in the Lord is not in vain. SPLIT INTO HOW MANY CHARACTERS PER SUBSETS: 10 OUTPUT: Therefore, my dear b rothers an d sisters, stand fir m. Let not hing move you. Alway s give you rselves fu lly to the work of t he Lord, b ecause you know that your labo r in the L ord is not in vain.

1942


What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

3722


how to create a 3x3 two dimensional array that will give you the sums on the left and bottom columns

3138






Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

2874


How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?

2032


could you please send the program code for multiplying sparse matrix in c????

3087


#include int main(void) { int a=4, b=2; a=b<>2 ; printf("%d",a); return 0; }

1086


Write a program to model an exploding firecracker in the xy plane using a particle system

3705


why nlogn is the lower limit of any sort algorithm?

2389


write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30

2232


write a program for area of circumference of shapes

2046


3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.

3336


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

2700