To print all combinations for the given string

To print all combinations for the given string

This program can print all combinations for the given string.

Logic:

  1. Get the input string from the user.
  2. Swap the kth element with the (k-1)th element.
  3. Swapping process continues up to index 0.
  4. Repeat step 2 and 3 for n times.
  5. Then Print all combinations of strings.

Program:

[code lang=”c”]
#include<stdio.h>
void main()
{
char a[20];
int i,j,n,k,temp;
clrscr();
printf("enter the string:");
gets(a);
n=strlen(a); //finds the length of the string
for(j=0;j<n;j++)
{
for(k=n-1;k>0;k–) //swap the kth index with (k-1)th upto the index 1
{
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
printf("%s\n",a); //print after swapping
}
}

getch();
}
[/code]

You might also like:

Program to print combinations of 0’s and 1’s

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *