c语言字符串中+ 怎么用_C和C ++中的字符串置换

c语言字符串中+ 怎么用

Here you will get program for permutation of string in C and C++.

在这里,您将获得用于在C和C ++中对字符串进行置换的程序。

Permutation means all possible arrangements of given set of numbers or characters. For a string with n characters can have total n! arrangements. Take below example.

排列是指给定的一组数字或字符的所有可能排列。 对于包含n个字符的字符串,其总数可以为n! 安排。 请看下面的例子。

c语言字符串中+ 怎么用_C和C ++中的字符串置换

Here we are using backtracking method to find the permutation of a string. Watch below video to understand how to do this.

在这里,我们使用回溯方法来查找字符串的排列。 观看下面的视频以了解操作方法。

Below I have shared the code to implement this method in C and C++.

下面,我共享了在C和C ++中实现此方法的代码。

C语言中的字符串置换程序 (Program for Permutation of String in C)

1
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
#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;
}
1
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
#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

输入字符串:abc abc acb bac bca cba cab

C ++中的字符串置换程序 (Program for Permutation of String in C++)

1
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;
}
1
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 ;
}

Comment below if you have doubts or found any information incorrect in above tutorial.

如果您对以上教程有疑问或发现任何信息不正确,请在下面评论。

Source: http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/

资料来源: http : //www.geeksforgeeks.org/write-ac-program-to-print-all-permutations-of-a-given-string/

翻译自: https://www.thecrazyprogrammer.com/2016/11/permutation-string-c.html

c语言字符串中+ 怎么用