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 for the second routine. 2 How many permutations for a string of length l?

31 0

Get full Expert solution in seconds

$1.97 ONLY

Unlock Answer

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 for the second routine. 2 How many permutations for a string of length l?

EXPERT ANSWER

#include<iostream>

using namespace std;


void permute(const string &a, int low, int high)
{
char temp;
string p = a;
if (low == high)
cout << a << endl;
else
{
for (int i = low; i <= high; i++)
{

temp = p[i];
p[i] = p[low];
p[low] = temp;

permute(p, low+1, high);

temp = p[i];
p[i] = p[low];
p[low] = temp;

}
}
}


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

int main(){
permute(“abc”);
return 0;
}