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
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 |
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 |
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 |
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 |
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 |
# 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 |
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 |
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 |
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 |
#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; }
main() { printf("%x",-1<<4); }
main() { int i, n; char *x = “girl”; n = strlen(x); *x = x[n]; for(i=0; i<n; ++i) { printf(“%s\n”,x); x++; } }
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.
How will u find whether a linked list has a loop or not?
main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }
9 Answers CSC, GoDB Tech, IBM,
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18.
main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); }
Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?
Write a function to find the depth of a binary tree.
13 Answers Adobe, Amazon, EFI, Imagination Technologies,
given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit
main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); }