Using the code stub below, output all combinations of character variables a, b, and c, in the order shown below. If a = 'x', b = 'y', and c = 'z', then the output is:
xyz xzy yxz yzx zxy zyx
Your code will be tested in three different programs, with a, b, c assigned with 'x', 'y', 'z', then with '#', '$', '%', then with '1', '2', '3’.
#include <iostream>
using namespace std;
int main() {
char a;
char b;
char c;
cin >> a;
cin >> b;
cin >> c;
/* Your solution goes here */
cout << endl;
return 0;
}
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void helper(char arr[], int n, int start)
{
if(start == n)
{
for(int i=0; i<n; i++)
{
cout<<arr[i];
}
cout<<endl;
return;
}
for(int i=start; i<n; i++)
{
swap(arr[i], arr[start]);
helper(arr, n,start+1);
swap(arr[i], arr[start]);
}
}
int main()
{
char arr[3];
for(int i=0; i<3; i++)
{
cout<<"Enter char"<<i+1<<" : ";
cin>>arr[i];
}
helper(arr,3,0);
}
Comments
Leave a comment