The permutation function goes through the whole string, swaps the first two characters and calls the same function recursively with same string but with an incremented starting index. The terminating condition of this recursive function is that when the passed starting index is same as the terminating index and the passed string is printed and to restore the previous state of the string as soon as the called recursive function is returned, the swapped characters are again re-swapped.
Here is source code of the C++ program which prints pascal’s triangle. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
* C++ Program to Find Permutations of Given Character String
using namespace std;
/* Function to swap two characters */
void swap(char& a, char& b)
temp = a;
a = b;
b = temp;
/* Function to obtain permutations of string characters */
void permutation(string s,int i,int n)
if (i == n)
cout << s << "\t";
for (j = i; j < s.length(); j++)
permutation(s, i + 1, n);
cout << "Enter the string : ";
cin >> s;
cout << endl << "The permutations of the given string : " << endl;
permutation(s, 0, s.length() - 1);
cout << endl;
$ g++ main.cpp $ ./a.out Enter the string : abc The permutations of the given string : abc acb bac bca cba cab
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.