Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment is to write a program that reads a set of strings from the stand

ID: 3821039 • Letter: T

Question

This assignment is to write a program that reads a set of strings from the standard input (keyboard), generates all permutations of that the strings, and then display the results to the standard output (screen).

You may use any of the standard library types (e.g., the containers that we learned in the class) in your implementation, including library functions from the <algorithm>library that generate permutations. You will need to study functions related to permutation in the library by yourself if you decide to do so. Or you may write code to generate permutations on your own.

Be sure to include the reference(s) that you learn the <algorithm> library or how to generate permutations.

A permutation is defined as an ordered arrangement of a set of objects. For example, the following set of three distinct strings would have six permutations:

Strings: “ab” “c” “de”

Permutations:

1. ab c de

2. ab de c

3. c ab de

4. c de ab

5. de ab c

6. de c ab

Here is some information about the algorithm library: http://www.cplusplus.com/reference/algorithm/

You may use the following code as a starting point. You’ll

need to implement generate_permutations and print_permutations functions.

int main(int argc,char* argv[])

{

list<string> inputList;

string aLine, aString;

//read in strings from stdin

cout<<"Enter strings, separated by a space:";

getline(cin, aLine);//read a line from keyboard

istringstream iss(aLine);//use

//parse each string

while(iss >> aString)

{

inputList.push_back(aString);

}

auto permutations = generate_permutations(inputList);

//print permutations to stdout

print_permutations(permutations);

return 0;

}

Extra Credits (10 points):

Assume that the input set of strings may contain duplicates, i.e. your input could be “ab”, “c”, “ab”. This set should produce only two permutations (“ab c” and “c ab”) since the second ab is a duplicate. Implement the extra functionality to handle duplicates in the input and generate the correct result.

Example test outputs:

Enter strings, separated by a space: 1 22 3

Permutations are:

1. 1 22 3

2. 1 3 22

3. 22 1 3

4. 22 3 1

5. 3 1 22

6. 3 22 1

Enter strings, separated by a space: a b cd ef

Permutations are:

1. a b cd ef

2. a b ef cd

3. a cd b ef

4. a cd ef b

5. a ef b cd

6. a ef cd b

7. b a cd ef

8. b a ef cd

9. b cd a ef

10. b cd ef a

11. b ef a cd

12. b ef cd a

13. cd a b ef

14. cd a ef b

15. cd b a ef

16. cd b ef a

17. cd ef a b

18. cd ef b a

19. ef a b cd

20. ef a cd b

21. ef b a cd

22. ef b cd a

23. ef cd a b

24. ef cd b a

The program does not handle the duplicates correctly (your result may differ depending on how the permutations are generated)

Enter strings, separated by a space: 1 22 1

Permutations are:

1. 1 22 1

2. 22 1 1

The program handles the duplicates correctly

Enter strings, separated by a space: 1 22 1

Permutations are:

1. 1 22

2. 22 1

Explanation / Answer

Hi,

Please see the below code that can find combination for the characters. Could not complete for the strings in the given time. Please modify and convert it for strings.

#include<stdio.h>
#include<string.h>
#define N 10

void print(char *num, int n)
{
int i;
for ( i = 0 ; i < n ; i++)
printf("%c ", num[i]);
printf(" ");
}
int main()
{
char num[N];
int *ptr;
char temp;
int i, n, j;
printf(" How many characters you want to enter: ");
scanf("%d", &n);
printf(" Enter a list of numbers to see all combinations: ");
for (i = 0 ; i < n; i++)
scanf("%c", &num[i]);
for (j = 1; j <= n; j++) {
for (i = 0; i < n-1; i++) {
temp = num[i];
num[i] = num[i+1];
num[i+1] = temp;
print(num, n);
   }
}
return 0;
}