C Language Read about permutations online Then write a funct

C++ Language:

Read about permutations on-line. Then write a function void permutations(std::string s) which generates all permutations of a string and prints them to stdout. Call this permute.cpp. Demonstrate the function in your main function.

Solution

#include <iostream>

using namespace std;

// Swap fuction is to swap two char
void swap(char& a, char& b)
{
char temp;
temp = a;
a = b;
b = temp;
}

// permutation fuction is to find the permutation of the string
// The permutation function goes through the whole string, swap funtion swaps the first two characters and calls the same function recursively with
//same string but with an incremented starting index.
void permutation(string word,int i)
{
int j;
if (i == word.length() - 1)
// print unique permuted word
cout << word << endl;
else
{
for (j = i; j < word.length(); j++)
{

cout << i << \" \" << j << endl;
swap(word[i],word[j]);
permutation(word, i + 1);
swap(word[i],word[j]);
}
}
}

int main()
{
string word;
cout << \"Enter the string : \";
cin >> word;
permutation(s, 0);
}

C++ Language: Read about permutations on-line. Then write a function void permutations(std::string s) which generates all permutations of a string and prints th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site