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() { extern int i; i=20; printf("%d",i); }

1 Answers   Value Labs,


Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique.

5 Answers   IITR, Microsoft, Nike,


main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } }

1 Answers  


int aaa() {printf(“Hi”);} int bbb(){printf(“hello”);} iny ccc(){printf(“bye”);} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); }

1 Answers  


abcdedcba abc cba ab ba a a

2 Answers  






Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%d",*p); return 0; } if input is 20 ,what will be print

2 Answers  


Write a routine to implement the polymarker function

0 Answers   TCS,


Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

0 Answers   IBM,


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,


What is data _null_? ,Explain with code when u need to use it in data step programming ?

0 Answers   Abbott,


A program that will create a movie seat reservation. The program will display the summary seats and its status. The user will be ask what seat no. to be reserved, then it will go back again to the summary to display the updated seat status. If the seat no. is already reserved then it will prompt an error message. And also if the input seat no is out of range then it will also prompt an error message. The program is continuously running. Termination of the program will depends on how the programmer will apply. Sample output: Movie Seats Reservation Summary of Seats: Seat 1: Available Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 6 The Seat no. is out of range! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 The Seat no. is already reserved! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 0 GoodBye... Thank You!!!

0 Answers  


why array index always strats wuth zero?

2 Answers  


Categories