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 use power function under linux environment.eg : for(i=1;i<=n;i++){ pow(-1,i-1)} since it alerts undefined reference to 'pow'.
func(a,b) int a,b; { return( a= (a==b) ); } main() { int process(),func(); printf("The value of process is %d !\n ",process(func,3,6)); } process(pf,val1,val2) int (*pf) (); int val1,val2; { return((*pf) (val1,val2)); }
In a gymnastic competition, scoring is based on the average of all scores given by the judges excluding the maximum and minimum scores. Let the user input the number of judges, after that, input the scores from the judges. Output the average score. Note: In case, more than two judges give the same score and it happens that score is the maximum or minimum then just eliminate two scores. For example, if the number of judges is 5 and all of them give 10 points each. Then the maximum and minimum score is 10. So the computation would be 10+10+10, this time. The output should be 10 because 30/3 is 10.
Derive expression for converting RGB color parameters to HSV values
How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);
Find your day from your DOB?
15 Answers Accenture, Microsoft,
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)
1 o 1 1 0 1 0 1 0 1 1 0 1 0 1 how to design this function format in c-language ?
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }
main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }
main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”