You are to write a program that creates permutations of an unknown number of str
ID: 3803359 • Letter: Y
Question
You are to write a program that creates permutations of an unknown number of strings of various sizes while removing duplicated permutations. For example, given the string "seed", all permutations are shown below with the duplicates crossed out: You program should only print out one instance of the duplicate. Print out all the permutations on one line (let it Wrap on its own) and leave one space between each permutation. Put one blank line between permutations sets. For example, your output for strings "seed" and "cats" should appear similar to below. The order of permutations is not important, but the words should be output in the same order as the data File. The data file is located at -dmk0080/public/1040/labs/exam l/permutewords and contain one word per line. Name your program premutations.c. You may use C or C + + style Strings, I/O. and even Dynamic Memory Management if necessary.Explanation / Answer
#include <stdio.h>
#include <string.h>
char all[10000][100];
int pos =0;
void swapChar(char *x, char *y);
void permute(char *string, int l, int r);
int main()
{
char str[] = "AAC";
int n = strlen(str);
permute(str, 0, n-1);
int i=0;
printf("All permutation : ");
for(i=0;i<pos;i++){
printf("%s ",all[i]);
}
printf(" ");
return 0;
}
void permute(char *string, int l, int r)
{
int i;
if (l == r){
// printf("%s ", string);
int p;
int found=0;
for(p=0;p<pos;p++){
if(strcmp(string,all[p])==0)
found=1;
}
if(!found){
strcpy(all[pos],string);
pos++;
}
}
else
{
for (i = l; i <= r; i++)
{
swapChar((string+l), (string+i));
permute(string, l+1, r);
swapChar((string+l), (string+i)); //backtrack
}
}
}
void swapChar(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.