``````#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char combine(char [],int k,int n);
int main()
{
char str[10];
int m,n;
printf("enter the string: ");
fgets(str,sizeof(str),stdin);
m=0;
n=strlen(str);
printf("the combinations are....\n");
combine(str,m,n-1);
return 0;
}
char combine(char a[],int k,int n)
{
int i;
char temp[40],t;
strcpy(temp,a);
if(k==n)
{
printf("%s\t",a);
}
else{
for(i=k;i<=n;i++)
{
t=a[k];
a[k]=a[i];
a[i]=t;
combine(a,k+1,n);
}
}
strcpy(a,temp);
}
``````

actually output will be like this
enter the string:ab
the occurences are
1.aa
2.bb
3.ab
4.ba

actually i didnt write this code i just copied from google.but it produces the correct output.but here from line 27 to line 33

``````for(i=k;i<=n;i++)
{
t=a[k];
a[k]=a[i];
a[i]=t;
combine(a,k+1,n);
}
``````

i dont understand wat happens here.please some one explain.

Just for the record, when someone talks about occurrences they almost never mean combinations. As for how this works, it recursively builds a tree that breaks down the string, swaps elements, and recombines them to produce combinations.

When you have an algorithm you don't understand, the best approach is to either step through a small data set in a debugger or on paper and draw out what happens to the data. For example, you can get a good test run of this algorithm by typing "abc".

Don't expect someone to be around to explain everything to you, a good programmer's best weapon is the ability to disect code, trace through it, and methodically figure out how it works without any help.