0% found this document useful (0 votes)
950 views1 page

C Program To Print All Permutations With Duplicates Allowed

This C program prints all permutations of a string with duplicates allowed. It includes functions to swap characters, permute a string by recursively swapping characters and calling the permute function, and a main function to get a string from the user and call permute. Permute takes the string, starting index, and ending index as parameters and recursively calls itself, swapping characters and printing the permutations.

Uploaded by

Spk Sudhin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
950 views1 page

C Program To Print All Permutations With Duplicates Allowed

This C program prints all permutations of a string with duplicates allowed. It includes functions to swap characters, permute a string by recursively swapping characters and calling the permute function, and a main function to get a string from the user and call permute. Permute takes the string, starting index, and ending index as parameters and recursively calls itself, swapping characters and printing the permutations.

Uploaded by

Spk Sudhin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

// C program to print all permutations with duplicates allowed

#include <stdio.h>
#include <string.h>
/* Function to swap values at two pointers */
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
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)); //backtrack
}
}
}
/* Driver program to test above functions */
Void main()
{
char str[80] ;
puts("Enter a string:\n\n");
gets(str);
int n = strlen(str);
permute(str, 0, n-1);
}

You might also like