C Exercise P1012 The following program generates all permuta
Solution
/*
* C++ Program to Find Permutations of Given Character String
*/
#include<iostream>
using namespace std;
/* Function to swap two characters */
void swap(char& a, char& b)
{
char temp;
temp = a;
a = b;
b = temp;
}
/* Function to obtain permutations of string characters */
void permutation(string s,int i,int n)
{
int j;
if (i == n)
cout << s << \"\\t\";
else
{
for (j = i; j < s.length(); j++)
{
swap(s[i],s[j]);
permutation(s, i + 1, n);
swap(s[i],s[j]);
}
}
}
int main()
{
string s;
cout << \"Enter the string : \";
cin >> s;
cout << endl << \"The permutations of the given string : \" << endl;
permutation(s, 0, s.length() - 1);
cout << endl;
}
Enter the string : chegg
The permutations of the given string : chegg chegg chgeg chgge chgge chgeg cehgg cehgg ceghg ceggh ceggh ceghg cgehg cgegh cgheg cghge cgghe cggeh cgegh cgehg cggeh cgghe cghge cgheg hcegg hcegg hcgeg hcgge hcgge hcgeg hecgg hecgg hegcg heggc heggc hegcg hgecg hgegc hgceg hgcge hggce hggec hgegc hgecg hggec hggce hgcge hgceg ehcgg ehcgg ehgcg ehggc ehggc ehgcg echgg echgg ecghg ecggh ecggh ecghg egchg egcgh eghcg eghgc egghc eggch egcgh egchg eggch egghc eghgc eghcg ghecg ghegc ghceg ghcge ghgce ghgec gehcg gehgc gechg gecgh gegch geghc gcehg gcegh gcheg gchge gcghe gcgeh ggech ggehc ggceh ggche gghce gghec ghegc ghecg ghgec ghgce ghcge ghceg gehgc gehcg geghc gegch gecgh gechg ggehc ggech gghec gghce ggche ggceh gcegh gcehg gcgeh gcghe gchge gcheg
