Write the routines with the following declarations: void permute( const string & str ); void permute( const string & str, int low, int high ); The first routine is a driver that calls the second and prints all the permutations of the characters in string str. If str is “abc”, then the strings that are output are abc, acb, bac, bca, cab and cba. Use recursion of the second routine.

32 0

Get full Expert solution in seconds

$1.97 ONLY

Unlock Answer

EXPERT ANSWER

Implementation in C++

#include <bits/stdc++.h>
using namespace std;
void permute(const string &str, int low, int high );
void permute(const string &str);

int main()
{
string str;
cout<<“Enter Input String:: “;
cin>>str;//To Read input String
permute(str);
return 0;
}

void permute(const string &str, int low, int high )
{
if (low== high)
cout<<str<<endl;
else
{
for (int i = low; i <= high ; i++)
{
string temp=str;
swap(temp[low], temp[i]);
permute(temp, low+1, high);
swap(temp[low], temp[i]);

}
}
}

void permute(const string &str){
int n = str.size();
permute(str, 0, n-1);
}