Enter a string and print out all permutations of the characters in the string

Source: Internet
Author: User
Tags printf

Title: Enter a string that prints all the permutations of the characters in the string.

Method: Use recursion to swap position, print output.

Code:

* * * main.cpp * *  Created on:2014.6.12 *
 author:spike/

/*eclipse CDT, gcc 4.8.1*/

#include <stdio.h>

void permutation (char* str, char* begin);

void permutation (char* str) {
    if (str = NULL) return
        ;
    Permutation (str, str);

void permutation (char* str, char* begin) {
    if (*begin = = ' ") {
        printf ("%s\n ", str);
        return;
    }

	for (char* C = begin; *c!= '; ++c ') {
		char temp = *c;
		*c = *begin;
		*begin = temp;

		Permutation (str, begin + 1);

		temp = *c;
		*c = *begin;
		*begin = temp;
	}

}

void Test (char* pstr)
{
    if (pstr = null)
        printf ("Test for null begins:\n");
    else
        printf ("Test for%s begins:\n", pstr);

    Permutation (PSTR);

    printf ("\ n");
}

int main (void)
{
    char str[] = "abc";
    Test (str);

    return 0;
}

Output:

Test for ABC begins:  
ABC  
ACB  
BAC  
BCA  
CBA  
cab

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Author: csdn Blog Caroline-wendy

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.