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:

#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();
}

You might also like:

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

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x