u

Thursday 5 January 2012

C++ Program to find all the combinations that you can make with one word




C++ Program to find all the combinations that you can make with one word example: "abc" -> 9 combinations -> abc - acb - bac - bca - etc...

#include <iostream.h>
#include <string.h>
// Program to display the all available
// unique combinations of a given string of any length.
#include <iomanip.h>
#include<conio.h>

typedef char string[80];

long fac(int getal)
//
{
  long faculteit = 1;

  for (; getal>1; getal--) faculteit*=getal;
  return faculteit;
} // long fac(int getal)

void swapchars(string tekst, int char1, int char2)
//
{
  char hulp;

  hulp=tekst[char2];
  tekst[char2]=tekst[char1];
  tekst[char1]=hulp;
} //

int findcombo(string tekst, int positie)
{
  int teller = 1;
  int nulpos = positie;
  long faculteit;
    string oldtekst;
  faculteit = fac(strlen(tekst)-positie);
  while (teller<=faculteit)
  {
    if (positie<strlen(tekst)-1) positie++;
    if (faculteit>2)
    {
      strcpy(oldtekst,tekst);
      teller+=findcombo(tekst,nulpos+1);
      strcpy(tekst,oldtekst);
      swapchars(tekst,nulpos,positie);
    }
    else
    {
      teller++;
      swapchars(tekst,nulpos,positie);
      cout << tekst << "\n";
    }
  }
  return faculteit;
} //
int main(void)
{
clrscr();
  string tekst;

  cout << "The entered words is : ";
  cin >> tekst;
  cout << endl<<"Word: " << tekst << "\n";
  cout << "Obtained combination: " << fac(strlen(tekst)) << "  ";

  findcombo(tekst,0);
  getch();
  return 0;
}
 

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More