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! 安排。 请看下面的例子。

Permutation of String in C and 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)

#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++)

#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语言字符串中+ 怎么用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值