C language Implementation Enter a string and print out all the permutations of the characters in the string _c language

Source: Internet
Author: User
Tags int size

This article describes the C language implementation of the input of a string to print out the character in the string of all permutations of the method, which belongs to the mathematical arrangement problem. is a very practical algorithm technique. Share to everyone for your reference. The implementation methods are as follows:

For example, the input string, ABC, outputs all the strings that can be arranged by character A, b, and C, ABC, ACB, BAC, BCA, Cab, and CBA.

The C language implementation code is as follows:

 
/* Copyright (c) alexingcool. All Rights Reserved. 
* * 
#include <iostream>
#include <algorithm>

using namespace std;

Char array[] = {' A ', ' B ', ' C '};
const int size = sizeof array/sizeof *array;

void Perm (char *array, int pos, int last) 
{ 
 if (pos = last) {
 copy (array, array + size, ostream_iterator< ;char> (cout, ""));
 cout << Endl;
 } 
 else { 
 for (int i = pos; I <= last; i++) { 
  swap (array[i], Array[pos]); 
  Perm (Array, pos + 1, last); 
  Swap (Array[i], Array[pos]);

}} void Main ()
{
 Perm (array, 0, 2);
}

It is hoped that the examples mentioned in this paper will be helpful to the learning of C program algorithm design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.