-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwheel_generator.c
More file actions
60 lines (57 loc) · 1.64 KB
/
Copy pathwheel_generator.c
File metadata and controls
60 lines (57 loc) · 1.64 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
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "wheel_generator.h"
void letter_swap(char* wheel,char value1,char value2){
char old= wheel[value1];
wheel[value1]=wheel[value2];
wheel[value2]=old;
}
int index_finder(char* wheel,char character){
if(strlen(wheel) == 0){
return -1;
}
for(int i=0; i<strlen(wheel);i++){
if(wheel[i]== character){
return i;
}
}
return -1;
}
void wheel_uniqueness(char* wheel1,char* wheel2){
int length = strlen(wheel1);
for(int i=0;i<strlen(wheel1);i++){
if(wheel1[i] == wheel2[i]){
while(wheel1[i]==wheel2[i]){
int random1=rand()%length;
int random2=rand()%length;
while(wheel1[random1] == wheel2[random2]){
random1=rand()%length;
random2=rand()%length;
}
letter_swap(wheel1,random1,i);
letter_swap(wheel2,random2,i);
}
}
}
}
char* wheel_generate(char* character_set){
int length = strlen(character_set);
char* wheel= (char *)calloc(length+1,sizeof(char));
if(wheel == NULL){
printf("Unable to allocate memory, quitting...\n");
exit(EXIT_FAILURE);
}
for(int i=0; i<length;i++){
char random_letter = character_set[rand()%length];
int index = index_finder(wheel,random_letter);
while(index != -1){
random_letter = character_set[rand()%length];
index=index_finder(wheel,random_letter);
}
wheel[i]=random_letter;
}
return wheel;
}