Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to generate all possible permutations of a string in C++

1 Answer

0 votes
#include <iostream>
 
void print_permutations(std::string s, int i, int len) {
   if (i == len) {
       std::cout << s << "\n";
   }
   else {
        for (int j = i; j <= len; j++) {
            std::swap(s[i], s[j]);
            print_permutations(s, i + 1, len);
            std::swap(s[i], s[j]);
       }
   }
} 
 
int main(void) {
   std::string s = "abcd";
   
   print_permutations(s, 0, s.size() - 1);
}
 
 
 
/*
 
abcd
abdc
acbd
acdb
adcb
adbc
bacd
badc
bcad
bcda
bdca
bdac
cbad
cbda
cabd
cadb
cdab
cdba
dbca
dbac
dcba
dcab
dacb
dabc
 
*/

 



answered Feb 16, 2021 by avibootz
edited Jan 5, 2025 by avibootz
...