递归算法---字符串全组合(面试宝典)

/**
 *求一字符串所有字串的组合
 */
#include <stdio.h>
#include <string.h>

void str_combine(char src[],char out[], int leng, int rec, int start)
{
        int i;
        for(i=start;i<leng;i++){
                out[rec]=src[i];
                out[rec+1]='\0';
                printf("%s ",out);
                if(i<leng-1)
                        str_combine(src,out,leng,rec+1,i+1);
        }
}
int main()
{
        char src[] = "hart";
        int leng = strlen(src);
        char out[sizeof(src)]={0};
        str_combine(src,out,leng,0,0);
        printf("\n");
        return 0;
}

相关推荐