Back to: C Tutorials For Beginners and Professionals
Permutation of Strings in C Language with Examples
In this article, I am going to discuss the Permutation of Strings in C Language with Examples. Please read our previous article where we discussed How to Check if 2 Strings are Anagram in C Language with Examples.
Permutation of Strings in C:
In this article, we will see how to find permutations of a string. First of all, we understand what does it mean means by permutations. Then we will see how to find permutations. There is more than one method. In this article, we’ll show you one method.
Here we have taken a string ‘ABC’. And we want all permutations with all arrangements of ‘ABC’. The total number of permutations or arrangements we can of string ‘ABC’ is n! which means 3! Which is 6 (3*2*1). So, 6 arrangements are possible for string ‘ABC’. Permutations are:
- ABC
- ACB
- BAC
- BCA
- CBA
- CAB
So, there is a total of six possible arrangements of those alphabets of string ‘ABC’. Now we want all these arrangements, so how to get this arrangement? For that, we will create some procedure or we will create some logic for generating all these permutations. First, let us explain some important things.
Here we are making a tree for the permutation of a string. So here the first alphabet is ‘A’.
The next alphabet is ‘B’ and the next alphabet is ‘C’. And we got the result ‘ABC’ if we come along this one. So, this is we’re forming a tree. Now from here go back. After that ‘B’ there was only one alphabet. Again, go back to ‘A’. We can go on ‘B’ as well as on ‘C’ so ‘B’ already we have taken. So let us go on ‘C’ then from ‘A’ we have gone on ‘C’ so what is remaining ‘B’ remaining:
Here ‘ACB’ will form. We got two different arrangements. So, we can show these arrangements in the form of a tree. After ‘A’, both ‘B’ and ‘C’ are over. So let us go to the next letter ‘B’:
The next letter should be either ‘A’ or ‘C’. First, we will take ‘A’ then we will take ‘C’:
So, there is another permutation formed that is ‘BAC’. Now go back, only ‘C’ was left as ‘B’ has already been taken.
Now we have got ‘BCA’. So let us go back to the main starting point and take the next letter ‘C’:
Now we have taken the remaining letter ‘C’. Now, what is remaining under ‘C’? ‘A’ and ‘B’ are remaining. So first we’ll go to ‘A’:
Here we got another permutation ‘CAB’. Now go back to ‘C’ and take ‘B’ as ‘A’ has already taken:
Now we got our last permutation ‘CBA’. So, we have got the six permutations. Here we did two things Back Tracking and Brute Force.
- Back Tracking means if we go back and take another possible route in a tree.
- Brute Force means finding out all possible permutations.
If we have any procedure and, in that procedure, we want to go back and take another route then those procedures can be implemented using Recursion. We have to use Recursion to achieve Back Tracking and with the help of Back Tracking, we are performing Brute Force. Now let’s see the code part:
Program for Permutation of Strings in C language by using Loop and Recursion:
Output:
Permutation of Strings Code in C language using loop and Recursion:
Output:
In the next article, I am going to discuss Structure in C Language with Examples. Here, in this article, I try to explain the Permutation of String in C Language with Examples. I hope you enjoy this Permutation of String in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.