Programming Algorithm-string arrangement code (C)

Source: Internet
Author: User

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


 

 

Related Article

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.