Best way to print all possible permutations of a string

Discussion in 'C++' started by sanket, Nov 29, 2011.

  1. sanket

    sanket Guest

    Hi all.

    I wrote a program to print all permutations of the letters of a
    string. Here are my two implementations

    1.

    include <iostream>
    #include <cstdlib>
    using namespace std;

    int main(int argc, char ** argv)
    {
    if(argc < 2) {cout << "Usage:\na.out <string>"<<
    endl;abort();}
    string s(argv[1]);

    for(string::iterator i = s.begin(); i != s.end(); i++)
    {
    for(string::iterator j = s.begin(); j != s.end(); j++)
    {
    if(j == i) continue;
    for(string::iterator k = s.begin(); k !=
    s.end(); k++)
    {
    if(k == j || k == i) continue;
    for(string::iterator p = s.begin(); p !
    = s.end(); p++)
    {
    if(p == k||p == j||p == i)
    continue;
    cout << *i << *j << *k << *p
    << endl;
    }
    }
    }
    }
    }

    2.

    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;

    int main(int argc, char ** argv)
    {
    if(argc < 2) {cout << "Usage:\na.out <string>"<<
    endl;abort();}
    string s(argv[1]);
    sort(s.begin(), s.end());
    cout << s << endl;
    while(next_permutation(s.begin(), s.end())){cout << s <<
    endl;}
    return 0;
    }

    They seem to be working well, but I was wondering if there was a
    better way to implement the same without using the STL function
    next_permutation.

    Thanks,
    Sanket
     
    sanket, Nov 29, 2011
    #1
    1. Advertising

  2. sanket

    Ian Collins Guest

    On 11/29/11 02:46 PM, sanket wrote:
    > Hi all.
    >
    > I wrote a program to print all permutations of the letters of a
    > string. Here are my two implementations


    <snip>

    > 2.
    >
    > #include<iostream>
    > #include<algorithm>
    > #include<cstdlib>
    > using namespace std;
    >
    > int main(int argc, char ** argv)
    > {
    > if(argc< 2) {cout<< "Usage:\na.out<string>"<<
    > endl;abort();}


    Calling abort is rather drastic, why not just "return EXIT_FAILURE"?

    > string s(argv[1]);
    > sort(s.begin(), s.end());
    > cout<< s<< endl;
    > while(next_permutation(s.begin(), s.end())){cout<< s<<
    > endl;}
    > return 0;
    > }
    >
    > They seem to be working well, but I was wondering if there was a
    > better way to implement the same without using the STL function
    > next_permutation.


    s/STL/standard library/

    Why would you want to avoid a library function that does what you want
    to do?

    --
    Ian Collins
     
    Ian Collins, Nov 29, 2011
    #2
    1. Advertising

  3. sanket

    sanket Guest

    On Nov 28, 8:01 pm, Ian Collins <> wrote:
    > On 11/29/11 02:46 PM, sanket wrote:
    >
    > > Hi all.

    >
    > > I wrote a program to print all permutations of the letters of a
    > > string. Here are my two implementations

    >
    > <snip>
    >
    > > 2.

    >
    > > #include<iostream>
    > > #include<algorithm>
    > > #include<cstdlib>
    > > using namespace std;

    >
    > > int main(int argc, char ** argv)
    > > {
    > >          if(argc<  2) {cout<<  "Usage:\na.out<string>"<<
    > > endl;abort();}

    >
    > Calling abort is rather drastic, why not just "return EXIT_FAILURE"?
    >
    > >          string s(argv[1]);
    > >          sort(s.begin(), s.end());
    > >          cout<<  s<<  endl;
    > >          while(next_permutation(s.begin(), s.end())){cout<<  s<<
    > > endl;}
    > >          return 0;
    > > }

    >
    > > They seem to be working well, but I was wondering if there was a
    > > better way to implement the same without using the STL function
    > > next_permutation.

    >
    > s/STL/standard library/
    >
    > Why would you want to avoid a library function that does what you want
    > to do?
    >
    > --
    > Ian Collins


    I am a newbie and wanted to see if my first implementation is optimal.
    I should have asked this,
    Is it possible to further optimize my first implementation?

    --Sanket
     
    sanket, Nov 29, 2011
    #3
  4. sanket

    DDD Guest

    On Nov 29, 4:24 pm, sanket <> wrote:
    > On Nov 28, 8:01 pm, Ian Collins <> wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On 11/29/11 02:46 PM, sanket wrote:

    >
    > > > Hi all.

    >
    > > > I wrote a program to print all permutations of the letters of a
    > > > string. Here are my two implementations

    >
    > > <snip>

    >
    > > > 2.

    >
    > > > #include<iostream>
    > > > #include<algorithm>
    > > > #include<cstdlib>
    > > > using namespace std;

    >
    > > > int main(int argc, char ** argv)
    > > > {
    > > >          if(argc<  2) {cout<<  "Usage:\na.out<string>"<<
    > > > endl;abort();}

    >
    > > Calling abort is rather drastic, why not just "return EXIT_FAILURE"?

    >
    > > >          string s(argv[1]);
    > > >          sort(s.begin(), s.end());
    > > >          cout<<  s<<  endl;
    > > >          while(next_permutation(s.begin(), s.end())){cout<< s<<
    > > > endl;}
    > > >          return 0;
    > > > }

    >
    > > > They seem to be working well, but I was wondering if there was a
    > > > better way to implement the same without using the STL function
    > > > next_permutation.

    >
    > > s/STL/standard library/

    >
    > > Why would you want to avoid a library function that does what you want
    > > to do?

    >
    > > --
    > > Ian Collins

    >
    > I am a newbie and wanted to see if my first implementation is optimal.
    > I should have asked this,
    > Is it possible to further optimize my first implementation?
    >
    > --Sanket


    You need an algorithm. And you can choose recursion or not.
     
    DDD, Nov 30, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish Sahani
    Replies:
    11
    Views:
    1,649
    Boris Borcic
    Jun 26, 2006
  2. darin dimitrov
    Replies:
    4
    Views:
    1,440
  3. anurag

    print all permutations of string

    anurag, Jul 20, 2006, in forum: C Programming
    Replies:
    20
    Views:
    1,782
    Mark P
    Jul 25, 2006
  4. anurag
    Replies:
    18
    Views:
    9,224
    karteek007
    Apr 17, 2009
  5. OL/2
    Replies:
    2
    Views:
    136
Loading...

Share This Page