
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to print all permutations of a given string
In this problem, we are given a string. Our task is to create a c program to print all permutations of a given string.
This program will find all possible combinations of the given string and print them.
Permutation is the arrangement of all parts of an object, in all possible orders of arrangement.
Let’s take an example to understand the problem,
Input
xyz
Output
xyz, xzy, yxz, yzx, zxy, zyx
Explanation
These are all permutations take in order.
To solve this problem, we will use backtracking i.e. taking each character of the string as the first character of the permutation and then sequentially choosing all remaining characters of the string one by one. And thus, printing all the permutations of the string.
Program to print all permutations of a given string
//Program to print all permutations of a given string −
Example
#include <iostream> using namespace std; void findPermutations(string str, int l, int r){ if (l == r) cout<<str<<" "; else{ for (int i = l; i <= r; i++){ swap(str[l], str[i]); findPermutations(str, l+1, r); swap(str[l], str[i]); } } } int main(){ string str = "WXYZ"; int n = str.size(); findPermutations(str, 0, n-1); return 0; }
Output
WXYZ WXZY WYXZ WYZX WZYX WZXY XWYZ XWZY XYWZ XYZW XZYW XZWY YXWZ YXZW YWXZ YWZX YZWX YZXW ZXYW ZXWY ZYXW ZYWX ZWYX ZWXY
- Related Articles
- Python Program to print all permutations of a given string
- Print all permutations of a given string
- Print all distinct permutations of a given string with duplicates in C++
- Print all palindrome permutations of a string in C++
- Print all the palindromic permutations of given string in alphabetic order in C++
- Print all permutations of a string in Java
- Program to print all substrings of a given string in C++
- Java Program to print distinct permutations of a string
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- C++ Program to Find the Number of Permutations of a Given String
- Print k different sorted permutations of a given array in C Program.
- How to find all possible permutations of a given string in Python?
- C++ Permutations of a Given String Using STL
- Python program to get all permutations of size r of a string

Advertisements