again. I’ve written a code on finding all possible combinations on a word and my code works but very limited times (i.e) it prints the string with “a” being only swapped (my I/P is abcd) but not b,c and d.
Code is duly attached. I plead you to help me.
#include<stdio.h>
void combination(char *,int,int);
int my_strlen(char *);
int main()
{
char s[10];
scanf("%[^n]",s);
int n=my_strlen(s);
combination(s,0,n-1);
return 0;
}
void combination(char *s,int i,int n)
{
char c[10];
for(int j=0;j<=n;j++)
c[j]=s[j];
int cn=0,k=0,l=0;
while(s[k]!='')
{
i=0,cn=0;
while(s[i]!='')
{
if(s[i]==c[i])
cn++;
i++;
}
if(--cn==n)
{
int m=l;
while(m<n)
{
char temp=s[m];
s[m]=s[m+1];
s[m+1]=temp;
printf("%sn",s);
m++;
}
l++;
}
k++;
}
}
int my_strlen(char *s)
{
int i=0,c=0;
while(s[i++]!='')
c++;
return c;
}
I knew that my logic is incomplete but I expect atleast 18 combinations instead of 24, but it isn’t going that way too (it prints only 3). So, I request my codies to help me complete my code, because I’ve just begun to delve deep into string concepts. I believe that controlling ‘k’ is the problem, but I couldn’t find the way to control it.