Displaying all permutations of a string I am using this code to display all the permutations of a given input string. Any advice/solution will be very helpful.

#include&lt;iostream&gt;
using namespace std;

/* Function to swap two characters */
void swap(char&amp; a, char&amp; 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 &lt;&lt; s &lt;&lt; "\t";
else
  {
    for (j = i; j &lt; s.length(); j++)
    {
        swap(s[i],s[j]);
        permutation(s, i + 1, n);
        swap(s[i],s[j]);
    } 
  }
}  
int main()
{
 string s;
 cout &lt;&lt; "Enter the amino acid string : ";
 cin &gt;&gt; s;
 cout &lt;&lt; endl &lt;&lt; "The permutations of the given string : " &lt;&lt; endl;
 permutation(s, 0, s.length() - 1);
 cout &lt;&lt; endl;
}
Note that using the standard library function std::next_permutation from the header &lt;algorithm&gt;, your code can more or less be reduced to:

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

int main()
{
    std::string s;
    std::cout &lt;&lt; "Enter the amino acid string: ";
    std::cin &gt;&gt; s;
    std::cout &lt;&lt; "\nThe permutations of the given string:\n";

    std::sort(s.begin(), s.end());
    do
    {
        std::cout &lt;&lt; s &lt;&lt; '\n';
    } while(std::next_permutation(s.begin(), s.end()));
}


Note that the iterable (the string in our case) needs to be sorted for the call to std::next_permutation to work, hence the call to std::sort beforehand.



That said, solving your problem in a more idiomatic manner might be interesting, but there are some more things you may want to know if you want to write idiomatic C++ in the future:


You don't need to write your own swap function. The standard already provides std::swap to swap any two values.
using namespace std; is often considered bad practice. While it shouldn't matter in your case, using it in a header file will drag every name from the std:: namespace into the global namespace in every file including this header file. Generally speaking, unless you know what you're doing, avoid this construct and fully qualify every name from the standard library with std::.
You could use some additional spacing. Don't hesitate to separate your functions by empty lines and to add some more spaces between your functions parameters. It will make your code easier to read. Also, try to be consistent when you indent stuff; use the same number of spaces for indentation everywhere.