Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Best way to print all possible permutations of a string

Reply
Thread Tools

Best way to print all possible permutations of a string

 
 
sanket
Guest
Posts: n/a
 
      11-29-2011
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-29-2011
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
 
Reply With Quote
 
 
 
 
sanket
Guest
Posts: n/a
 
      11-29-2011
On Nov 28, 8:01*pm, Ian Collins <(E-Mail Removed)> 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
 
Reply With Quote
 
DDD
Guest
Posts: n/a
 
      11-30-2011
On Nov 29, 4:24*pm, sanket <(E-Mail Removed)> wrote:
> On Nov 28, 8:01*pm, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print all permutations of string anurag C++ 18 04-17-2009 02:48 PM
print all permutations of string anurag C Programming 20 07-25-2006 06:47 PM
How to generate all permutations of a string? Girish Sahani Python 11 06-26-2006 06:16 PM
How to generate all possible permutations with repetitions? darin dimitrov C Programming 4 10-27-2004 08:18 AM
printing all possible permutations of a set of characters OL/2 Perl Misc 2 05-26-2004 04:06 PM



Advertisments