打印字符串的全排列

来源:互联网 发布:家居装修 淘宝 编辑:程序博客网 时间:2024/05/17 02:27

题目:打印字符串的全排列。如:“hat”,则打印hat hta aht ath tah tha.

 

代码如下:

 

void f(char *str, int totalLen)

{

    int strLen = strlen(str);

    if (1 == strLen)

    {

        printf("%s/n", str - totalLen  + 1);

        return;

    }

    for (int i = 0; i < strLen; i++)

    {

        char temp = str[0];

        str[0] = str[i];

        str[i] = temp;

        f(str + 1, totalLen);

        str[i] = str[0];

        str[0] = temp;

    }

}

原创粉丝点击