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