Tuesday, December 12, 2017

C++ Code Review: Shows permutation of entered letters

Description: This program shows all possible combination of entered letters.

Code:

//Compiling Tool: Code::Block IDE with MinGW

//Program shows all possible combination of entered letters.

 

#include<iostream>

#include<string.h>

 

using namespace std;

 

void swap(char *x, char *y)

{

    char temp;

    temp = *x;

    *x = *y;

    *y = temp;

}

 

void permutation(char *a, int l, int r)

{

   int i;

 

   if (l == r)

     cout << a << "\n";

   else

   {

       for (i = l; i <= r; i++)

       {

          swap((a+l), (a+i));

          permutation(a, l+1, r);

          swap((a+l), (a+i));

       }

   }

}

 

int main()

{

    char string[20];

    int n;

 

    cout << "Enter a string: ";

    cin >> string;

 

    n = strlen(string);

    permutation(string, 0, n-1);

 

    return 0;

}          

 Output:

 clip_image002

If you want to write or construct or program C++ mini-project and do not know how or from where to start buy this simple e-book: Code Review of 26 C++ mini-projects. For book sample click this link.

Add your comments in below available comment box.

Note: Click these label/tags to view all related posts. Tags: C++Code

 

…till next post, bye-bye & take care.

No comments:

Post a Comment