Question Name:Permutations

#include <stdio.h>
#include <string.h>
void swap(char *x, char *y)
{
	char temp;
	temp = *x;
	*x = *y;
	*y = temp;
}
void permute(char *a, int l, int r)
{
int i;
if (l == r)
	printf("%s\n", a);
else
{
	for (i = l; i <= r; i++)
	{
		swap((a+l), (a+i));
		permute(a, l+1, r);
		swap((a+l), (a+i)); 
    }
}
}
int main()
{
	char str[100];
   scanf("%s",str);
	int n = strlen(str);
	permute(str, 0, n-1);
	return 0;
}
  • Problem Description
    Given numbers 1,2,3. Write a C Program to Generate All Permutations of the given numbers using BackTracking.
  • Test Case 1
    Input (stdin)123
    Expected Output123
    132
    213
    231
    321
    312
  • Test Case 2
    Input (stdin)9385
    Expected Output9385
    9358
    9835
    9853
    9583
    9538
    3985
    3958
    3895
    3859
    3589
    3598
    8395
    8359
    8935
    8953
    8593
    8539
    5389
    5398
    5839
    5893
    5983
    5938

Leave a Reply

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

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.