none
permutation of a string RRS feed

  • Question

  •  

    can some1 give the algo or the code(in C) for displaying all the permutation of a word...eg for "car", it has to print c,a,r,ca,ac,cr,car,arc etc ...

    Wednesday, December 26, 2007 12:18 PM

All replies

  • A simple solution, might not be the most efficient way to go about it:

     

    Code Block

    void permuteString(char *strInput, const int pos)

    {

    for (int i = pos + 1; i < sizeof(strInput)-2; i++)

    {

    for (int j = i+1; j < sizeof(strInput) - 1; j++)

    {

    swap<char>(strInput[i],strInput[j]);

    permute(strInput, i);

    swap<char>(strInput[i],strInput[j]);

    }

    }

    printf("%s\n", strInput);

    }

     

    Sunday, January 20, 2008 6:21 PM
  • thnx
    Wednesday, January 23, 2008 5:11 PM
  • Gud dear

     

    Monday, January 28, 2008 12:02 PM