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

Answers were Sorted based on User's Feedback



How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / raghuram.a

#include<stdio.h>
long pow(long a,long b) /*finds a power of b*/
{
long i,j=1;
for(i=1;i<=b;i++)
j*=a;
return j;
}
long bin(int n) /*converts into binary equivalent */
{
long i=0,j=1,r;
while(n)
{
r=n%2;
i=i+r*j;
n/=2;
j*=10;
}
return i;
}
main()
{
char a[25];
long i,j,k,m,l=0,n=0;
printf("\nEnter string:");
scanf("%s",a);
printf("\nAll possible substrings are:\n");
while(a[l])
l++;
printf("%d)\t{ }\n",++n);
for(i=1;i<pow(2,l);i++)
{ k=bin(i);
m=l-1;
printf("%d)\t{ ",++n);
while(m>=0)
{
j=k/pow(10,m);
if(j==1)
printf("%c",a[l-1-m]);

k=k%pow(10,m);
m--;
}
printf("\t}\n");
}
return 0;
}

Is This Answer Correct ?    7 Yes 0 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / raghuram.a

#include<stdio.h>
#include<conio.h>
long pow(long a,long b) /*finds a power of b*/
{
long i,j=1;
for(i=1;i<=b;i++)
j*=a;
return j;
}
long bin(int n) /*converts into binary equivalent */
{
long i=0,j=1,r;
while(n)
{
r=n%2;
i=i+r*j;
n/=2;
j*=10;
}
return i;
}
main()
{
char a[25];
long i,j,k,m,l=0,n=0;
printf("\nEnter string:");
scanf("%s",a);
printf("\nAll possible substrings are:\n");
while(a[l])
l++;
printf("%d)\t{ }\n",++n);
for(i=1;i<pow(2,l);i++)
{ k=bin(i);
m=l-1;
printf("%d)\t{ ",++n);
while(m>=0)
{
j=k/pow(10,m);
if(j==1)
printf("%c",a[l-1-m]);

k=k%pow(10,m);
m--;
}
printf("\t}\n");
}
getch();
return 0;
}



Is This Answer Correct ?    7 Yes 1 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / gordon roberts

Here is a trivial Java function to do this. At least two
of the above answers do not work at all...??? The binary
solution seems to have some merit but why make it so
difficult and problem frought??? I put in an element
counter to verify the correct number of elements in the set
upon print out. Call the function below using the obvious
form:

printSubsets("", "abc");

static int m_cElements = 1;
private static void printSubsets(String prefix, String str)
{
if(str.equals(""))
System.out.println((m_cElements++)+": {"+prefix+"}");
else
{
printSubsets(prefix, str.substring(1));
printSubsets(prefix+str.substring(0,1), str.substring
(1));
}
}

Is This Answer Correct ?    5 Yes 0 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / 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

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / a

aa

Is This Answer Correct ?    15 Yes 12 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / shashanktrip

int combine(char in[])
{

int len;
char *out;
len= strlen(in);
printf("<<<<<<<<Combinations>>>>>>");
out=(char *)malloc(sizeof(char)*(len+1));
if(!out)return -1;

DoCombine(in,out,len,0,0);
free(out);
return 1;
}


void DoCombine(char *in, char *out, int len, int
recLevel,int start)
{
int i;
for(i=start;i<len;i++)
{
out[recLevel]=in[i];
out[recLevel+1]='\0';
printf("\n%s\n",out);
if(i<len-1)

DoCombine(in,out,len,recLevel+1,i+1);
}
}

Is This Answer Correct ?    10 Yes 7 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / vadivel

# include<stdio.h>
# include<conio.h>
# include<alloc.h>
# include<string.h>
# include<math.h>

int bin[16];
void genbin(int);

void main()
{
clrscr();
char *str;
int i=0,len=0;
str = (char*)malloc(100);
printf("Input a no. :");
scanf("%s",str);
len = pow(2,strlen(str));
for(i=0;i < len;i++)
{
genbin(i);
for(int j=0;j < strlen(str);j++)
{
if(bin[j] == 1)
printf("%c",str[j]);
}
printf("\n");
}
free(str);
getch();
}
void genbin(int n)
{
for(int i=0;i<8;i++)
{
bin[i] = n%2;
n /= 2;
}
}

Is This Answer Correct ?    3 Yes 0 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / nkbinglei

#include <iostream>
#include <string>
using namespace std;
#include<math.h>

int main(int argc, char* argv[])
{
char input[100];

cout<<"input a string:"<<endl;
gets(input);
cout<<input<<endl;
int strLen = strlen(input);
int count = pow(2.0,strLen);
string *str = new string[count];
int num=0;
cout<<"{}"<<endl;
for(int i=0;i<strLen;i++)
{
str[num] = str[num]+input[i];
cout<<"{"<<str[num]<<"}"<<endl;
num++;
for(int j=0;j<i;j++)
{
str[num] = str[j]+','+input[i];
cout<<"{"<<str[num]<<"}"<<endl;
num++;
}
}

}

Is This Answer Correct ?    5 Yes 5 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / vijay nag

find the no of bits required to represent the value n.i.e.
no of elements in the set.This can be done using following
algorithm.
int bin(int n)
{
if(n==1)return 1;
return 1_n/2_1+1;
}
print(char c[12],int b)
{
for(int i=0;i<b;i++)
if(c[i]=='1')
printf("%c",65+i);
}

combinations(int n)
{
int b=bin(n);
char c[12];
for(int i=0;i<pow(2,b);i++)
{
for(int j=1;j<b;j++)
if(i>>1)
c[j-1]='1';
else
c[j-1]='0';

print(c,b);
}

Is This Answer Correct ?    1 Yes 1 No

How do I write a program to print proper subset of given string . Eg :input: abc out..

Answer / lakshmeeprasad

#include<stdio.h>
#include<string.h>
void main()
{
char str[10];
int len=0,i,j,k;
printf("Enter String......: ");
scanf("%[^\n]",str);
printf("\n\nEntered String is......: ");
puts(str);
len = strlen(str);
printf("\nlength=%d\n",len);
for(i=0 ; i<len ; i++)
{
for(j=i ; j<len ; j++)
{
for(k=i ; k<=j ; k++)
printf("%c",str[k]);
printf("\n");
}
printf("\n");
}
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Code Interview Questions

main() { printf("%d", out); } int out=100;

3 Answers  


Write a c program to search an element in an array using recursion

1 Answers   Wipro,


abcdedcba abc cba ab ba a a

2 Answers  


write a c program to Create a registration form application by taking the details like username, address, phone number, email along with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 5 users and display the details. In place of password display “****”. (Use Structures).

0 Answers   CDAC, College School Exams Tests,


#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }

3 Answers  






Write a program to print a square of size 5 by using the character S.

6 Answers   Microsoft,


main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }

1 Answers  


Finding a number multiplication of 8 with out using arithmetic operator

8 Answers   Eyeball, NetApp,


main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }

2 Answers  


what is the output of the below program & why ? #include<stdio.h> void main() { int a=10,b=20,c=30; printf("%d",scanf("%d%d%d",&a,&b,&c)); }

6 Answers   CSC, IIIT,


find A^B using Recursive function

2 Answers  


void main () { int x = 10; printf ("x = %d, y = %d", x,--x++); } a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above

2 Answers   HCL,


Categories