Write a c++ program that prints all possible distinct “words” that can be obtained by permuting the letters of the word input by users. (Note: A “word” here refers to a sequence of letters and need not be in the dictionary.)
For example:
Input word to find permutations: abc
acb
bac
bca
cba
cab
CODE IN C++ :
#include <iostream>
using namespace std;
// helper function
void printPermutationsHelper(string input, string output) // output
stores the permutation to be printed
{
if(input.length() == 0) // input string is empty
{
cout << output << endl; // print the output
return;
}
for(int i = 0; i <= output.length(); i++) // getting
permutations
{
printPermutationsHelper(input.substr(1), output.substr(0,i) +
input[0] + output.substr(i));
}
}
void printPermutations(string input)
{
printPermutationsHelper(input, ""); // helper function called
return;
}
// Driver's code to test the function
int main()
{
printPermutations("abc");
return 0;
}
OUTPUT SNIPPET :
Hope this resolve syour doubt.
Please give an upvote if you liked my solution. Thank you :)
Get Answers For Free
Most questions answered within 1 hours.