Tuesday, January 18, 2011

word combination / string combination








#include<stdio.h>
#include<string.h>
#define MAX 100


void
swap(char* src, char* dst)
{

char
ch = *dst;
*
dst = *src;
*
src = ch;
}


int
permute(char *set, int begin, int end)
{


int
i;
int
range = end - begin;

if
(range == 1)
{

printf("%s\n", set);
}


else


{

for
(i = 0; i < range; i++)
{


swap(&set[begin], &set[begin + i]);

permute(set, begin + 1, end);

swap(&set[begin], &set[begin + i]); /*set back*/

}
}
}


void
main()
{

char
str[MAX];

printf("Enter the string: ");

/*as ussual warnings with gets*/

gets(str);
permute(str, 0, strlen(str));

}

No comments:

Post a Comment