0

This is the code to print the Permutations of a String.

#include<stdio.h>
#include<conio.h>
#include<string.h>

void permute(char *str,int l,int pos,int r);
void swap(char &a,char &b);
void print_string(char *str,int r);

int main()
{
   char str[10]="";
   int l,r;
   printf("Enter The String : ");
   gets(str);
   l=strlen(str);
   printf("Enter The Number Of Places To permute on : ");
   scanf("%d",&r);
   printf("The Following Permuations are possible : \n\n");
   permute(str,l,1,r);
   getch();
   return 0;
}

void permute(char *str,int l,int pos,int r)
{
   //If lock position is on the next character
   //than the limit
   if(pos==r+1)
   {
       print_string(str,r); //print - these are the elements//
       printf(" ");
       return; //and return//
   }
   //true subscript of character in array is pos-1//
   for(int i=pos-1;i<=l-1;i++)
   {
       //swap the first letter with all next letters
       str[pos-1]=str[pos-1]+str[i]-(str[i]=str[pos-1]);
       permute(str,l,pos+1,r);
       //restore the swap{swap : a=a+b-(b=a)}
       str[pos-1]=str[pos-1]+str[i]-(str[i]=str[pos-1]);
   }
}

void print_string(char *str,int r)
{
   for(int i=0;i<r;i++)
       printf("%c",str[i]);
}
2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by Salem
0

> str[pos-1]=str[pos-1]+str-(str=str[pos-1]);
Perhaps use your swap function, with a temporary, rather than this over elaborate mess?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.