Sunday 8 April 2012

21).11Write a C program to print all permutations of a given string

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#include<math.h>
void permute(char*string,int i,int n) ;
void main()
{
   clrscr();
   char string[]="ABC";
   cout<<string;

    permute(string,0,2);

    getchar();
    getch();

}
void swap(char*x,char*y)
{
 char temp;
 temp=*x;
 *x=*y;
 *y=temp;
}


void permute(char*string,int i,int n)
{
  if(i==n)
  {
    cout<<"\n"<< string;

  }
  else
  {
    for(int j=i;j<=n;j++)
    {
        swap((string+i),(string+j)) ;
        permute(string,i+1,n);
        swap((string+i),(string+j)) ;
    }
  }

}

No comments:

Post a Comment

Write a program which reads a set of integers into an integer array and then prints "YES"if all of them are same otherwise print "NO".

#include<stdio.h> int main() { int a[10],M=0,i,n; printf("enter a value for n\n"); scanf("%d",&n); fo...