Here you will get the program for change of string in C and C++.
Stage implies every conceivable game plan of a given arrangement of numbers or characters. For a string with n characters can have complete n! game plans. Take underneath the model.
Here we are utilizing a backtracking technique to discover the stage of a string. Watch underneath the video to see how to do this.
Beneath I have shared the code to execute this technique in C and C++.
Program for Permutation of String in C
#include <stdio.h>
#include <string.h>
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permutation(char *a, int l, int r)
{
int i;
if (l == r)
printf("%s\n", a);
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permutation(a, l+1, r);
swap((a+l), (a+i));
}
}
}
int main()
{
char string[20];
int n;
printf("Enter a string: ");
scanf("%s", string);
n = strlen(string);
permutation(string, 0, n-1);
return 0;
}
Output
Enter a string: abc
abc
acb
bac
bca
cba
cab
Program for Permutation of String in C++
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string.h>
using namespace std;
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permutation(char *a, int l, int r)
{
int i;
if (l == r)
cout << a << "\n";
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permutation(a, l+1, r);
swap((a+l), (a+i));
}
}
}
int main()
{
char string[20];
int n;
cout << "Enter a string: ";
cin >> string;
n = strlen(string);
permutation(string, 0, n-1);
return 0;
}
Source: https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/