expected output: "abc" --> abc,acb,bca,bac,cab,cba
example outputs: when passed string s="abc" i get: cc ccc ccc
the (buggy) code is:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
string subS(string fullS, int s,int len);
string permut(string s ,int n);
void swap(string s,int a,int b);
int main (int argc, string argv[])
{
if (argc!=2)
printf("usage: ./permutation 5");
int n = strlen(argv[1]);
permut(argv[1],n);
}
string subS(string fullS, int s,int len)
{
string sub = fullS;
for(int j=0;j<len;j++)
{
sub[j]=fullS[s];
s=s+1;
}
sub[len]='\0';
return sub;
}
string permut(string s ,int n)
{
if (s!=NULL)
{
if(n==1)
return s;
else
{
string dummy_s=s;
string sub=subS(dummy_s,1,n-1);
string smaller=permut(sub,n-1);
string final = strncat(smaller,s,1);
for (int i=n-1;i>0;i--)
{
printf("%s\n",final);
swap(final,i,i-1);
}
}
}
return s;
}
void swap(string s,int a,int b)
{
if (a<=b)
return;
else
{
char c= s[a];
s[a]=s[b];
s[b]=c;
}
}
What I have tried:
have tried to provide the logic of my code throug comments.Not sure what i should add here.