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. }

advertisement
$ 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

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

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn