Subset of the word

QUESTION

Write a C program to find out the subsets of the word ‘programming’. For example, some of the subsets of the given word is,p,r, o, g, r, a , m, m ,i, n, g, pr, po, pg, pr, pm, rammin, etc.

ANSWER

#include <stdio.h>
 
char string[50], n;
void subset(int, int, int);
 
int main()
{
    int i, len;
 
   // printf("Enter the len of main set : ");
    scanf("%d", &len);
  //  printf("Enter the elements of main set : ");
    scanf("%s", string);
    n = len;
 //   printf("The subsets are :\n");
    for (i = 1;i <= n;i++)
        subset(0, 0, i);
  return 0;
}
 
/*Function to find the number of subsets in the given string*/
 
void subset(int start, int index, int num_sub)
{
    int i, j;
    if (index - start + 1  ==  num_sub)
    {
        if (num_sub  ==  1)
        {
            for (i = 0;i < n;i++)
                printf("%c\n", string[i]);
        }
        else
        {
            for (j = index;j < n;j++)
            {
                for (i = start;i < index;i++)
                    printf("%c", string[i]);
                printf("%c\n", string[j]);
            }
            if (start != n - num_sub)
                subset(start + 1, start + 1, num_sub);
        }
    }
    else
    {
        subset(start, index + 1, num_sub);
    }
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.