In: Computer Science
----------------
Exercise 2: String Permutations
----------------
Create a program that takes a string from the command line and prints every permutation of that string. You may assume the string will contain all unique characters. You may print the permutations in any order, as long as you print them all.
----------------
Output:
----------------
$>./prog dog d do dog dg dgo o od odg og ogd g gd gdo go god
----------------
I have no idea on coding this. The files I need for this exercise are main.cpp, makefile, Executive.cpp, and Executive.h. So, will someone help me on this exercise? If so, then that will be great.
//.................................//
#include <bits/stdc++.h>
using namespace std;
void printResult(char* result, int len);
void stringsCombination(char result[], char str[], int count[],int
level, int
size, int length) ;
void pCharCombination(string str);
int main(){
string str ;
cin>>str;
pCharCombination(str);
return 0;
}
/*..........................................................*/
void stringsCombination(char result[], char str[], int count[],int
level, int size, int length){
if (level == size)
return;
for (int i = 0; i < length; i++) {
if (count[i] == 0)
continue;
count[i]--;
result[level] = str[i];
printResult(result, level);
stringsCombination(result, str, count, level + 1, size,
length);
count[i]++;
}
}
//................................//
void pCharCombination(string str){
map<char, int> mp;
for (int i = 0; i < str.size(); i++) {
if (mp.find(str[i]) != mp.end())
mp[str[i]] = mp[str[i]] + 1;
else
mp[str[i]] = 1;
}
char* input = new char[mp.size()];
int* count = new int[mp.size()];
char* result = new char[str.size()];
map<char, int>::iterator it = mp.begin();
int i = 0;
for (it; it != mp.end(); it++) {
input[i] = it->first;
count[i] = it->second;
i++;
}
int length = mp.size();
int size = str.size();
stringsCombination(result, input, count, 0, size, length);
}
/*................................*/
void printResult(char* result, int len){
for (int k = 0; k <= len; k++)
cout<<result[k];
cout<<endl;
}
save your file with - filename.cpp