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 |
how to test pierrot divisor
write a origram swaoing valu without 3rd variable
main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }
what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }
#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }
How to reverse a String without using C functions ?
33 Answers Matrix, TCS, Wipro,
main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }
How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)
#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }
what is brs test reply me email me kashifabbas514@gmail.com
main() { int *j; { int i=10; j=&i; } printf("%d",*j); }
void main() { int i=5; printf("%d",i+++++i); }