Code for sorting strings (C)
Question: enter a string to print all the characters in the string.
Method: use recursion to switch positions sequentially and print the output.
Code:
/* * main.cpp * * Created on: 2014.6.12 * Author: Spike *//*eclipse cdt, gcc 4.8.1*/#include
void Permutation(char* pStr, char* pBegin);void Permutation(char* pStr){ if(pStr == NULL) return; Permutation(pStr, pStr);}void Permutation(char* pStr, char* pBegin){ if(*pBegin == '') { printf(%s, pStr); } else { for(char* pCh = pBegin; *pCh != ''; ++ pCh) { char temp = *pCh; *pCh = *pBegin; *pBegin = temp; Permutation(pStr, pBegin + 1); temp = *pCh; *pCh = *pBegin; *pBegin = temp; } }}void Test(char* pStr){ if(pStr == NULL) printf(Test for NULL begins:); else printf(Test for %s begins:, pStr); Permutation(pStr); printf();}int main(void){ char str[] = abc; Test(str); return 0;}
Output:
Test for abc begins:abcacbbacbcacbacab