输入一个字符串并打印出该字符串中字符的所有排列2015-10-14题目: 输入一个字符串, 打印出该字符串中字符的所有排列.方法: 使用递归依次交换位置, 打印输出.代码:
/* * 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
", 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:
");elseprintf("Test for %s begins:
", pStr);Permutation(pStr);printf("
");}int main(void){char str[] = "abc";Test(str);return 0;}
输出:
Test for abc begins:abcacbbacbcacbacab
作者:csdn博客 Caroline-Wendy