C Program to Find All Possible Subsets of Given Length in String

This C Program Find all possible subsets of given length in string.

Here is source code of the C Program to Find all possible subsets of given length in string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find All Possible Subsets of given Length in String
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. char a[20];
  8. int n, len, j;
  9.  
  10. void main()
  11. {
  12.     int i, index = 0, start = 0;
  13.  
  14.     printf("Enter the string\n");
  15.     scanf("%s", a);
  16.     n = strlen(a);
  17.     printf("enter input length\n");
  18.     scanf("%d", &len);
  19.     printf("The subsets are\n");
  20.     for (i  =  1;i < = n;i++)
  21.     {
  22.         if (index - start + 1  ==   i)
  23.         {
  24.             if (i  ==   len)
  25.             {
  26.                 for (j  =  index;j < n;j++)
  27.                 {
  28.                     for (i  =  start;i < index;i++)
  29.                         printf("%c", a[i]);
  30.                     printf("%c\n", a[j]);
  31.                 }
  32.                 if (start != i)
  33.                 {
  34.                     start++;
  35.                     index = start;
  36.                 }
  37.             }
  38.             else
  39.             {
  40.                 index++;
  41.             }
  42.         }
  43.     }
  44. }

$ cc string20.c
$ a.out
Enter the string
programming
enter input length
2
The subsets are
pr
po
pg
pr
pa
pm
pm
pi
pn
pg
enter the string
programming
enter input length
4
The subsets are
prog
pror
proa
prom
prom
proi
pron
prog

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.