In this article, you’ll learn how to find all permutations of a given string using C++, Python, JavaScript, and C.

How Do Permutations Work?

Let’s say you have string str with “MUO” as the string values. You’ve been asked to show the string’s permutations. Here’s how you’d go about it:

Example 1: Let str = “MUO”

The permutations of “MUO” are:

“MUO” “MOU” “UMO” “UOM” “OUM” “OMU”

Note the order of the values. Here’s another example:

Example 2: Let str = “AB”

All the permutations of “AB” are:

“AB” “BA”

You can also print duplicate permutations if there are repeating characters in the given string. (ABBA, for example)

Now that you understand how permutations work, let’s take a look at how you can find them using your preferred programming language.

Note: We’ve designed the following code examples to output permutations for three strings: MUO, AB, and XYZ. If you’d like to use any of this code, copy it, and change these strings to fit your project.

C++ Program to Print All Permutations of a String

Below is the C++ program to print all permutations of a string:

Output:

Python Program to Print All Permutations of a String

Next, is the Python code to print all permutations of a string:

Output:

JavaScript Program to Print All Permutations of a String

Here’s how you print permutations in JavaScript:

Output:

C Program to Print All Permutations of a String

Below is a C program that prints all permutations of a string:

Output:

Printing Permutations Is Easy

In this article, you’ve learned how to print all permutations of a string in several programming languages. While these sample programs aren’t the only way to handle permutations, they’re a great start for those who are new to using them in their code.