-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringPermutation.cpp
More file actions
48 lines (40 loc) · 1.11 KB
/
Copy pathstringPermutation.cpp
File metadata and controls
48 lines (40 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;
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 << *set << endl;
cout << set << endl;
} 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
}
}s
return 0;
}
//Example Implementation -- Up to you on how to use
int main()
{
float i = clock();
char str[255]; //string
cout << "Please enter a string: ";
cin.getline(str, 255); //take string
permute(str, 0, strlen(str)); //permutate the string
float j = clock();
float diff = j-i;
float seconds = diff / CLOCKS_PER_SEC;
cout << seconds;
system("Pause");
return 0;
}