You are on page 1of 2

String Permutation

#include <iostream.h> #include <string.h> #include<conio.h> #include<stdio.h> int k=1; void swap(char* first, char* second) { char ch = *second; *second = *first; *first = ch; }

int permute(char* set, int begin, int end) { int i; int range = end - begin; if (range == 1) { cout <<k<<" "<< set << endl; k++; } else { for(i=0; i<range; i++) { swap(&set[begin], &set[begin+i]); //initial swap

permute(set, begin+1, end); //recursion swap(&set[begin], &set[begin+i]); //swap back } } return 0; }

//Example Implementation -- Up to you on how to use int main() { clrscr(); char str[255]; //string cout << "Please enter a string: "; cin.getline(str, 255); //take string permute(str, 0, strlen(str)); //permutate the string getch(); return 0; }

You might also like