-
Notifications
You must be signed in to change notification settings - Fork 0
/
shufflechars.c
59 lines (45 loc) · 1.12 KB
/
shufflechars.c
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
/*
* Copyright (C) 2015 Tomasz Kramkowski <tk@the-tk.com>
*
* This program is free software. It is licensed under version 3 of the
* GNU General Public License.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
static inline unsigned rand_to_max(unsigned max)
{
double random;
while ((random = rand()) == (double)RAND_MAX);
return random / (double)RAND_MAX * (double)max;
}
int main(int argc, char **argv)
{
size_t length;
char *text, *dest;
srand(time(NULL));
if (argc != 2) {
fprintf(stderr, "Usage: %s <text>\a\n", argv[0]);
exit(EXIT_FAILURE);
}
text = argv[1];
length = strlen(text);
bool relocated[length];
memset(relocated, 0, length);
dest = malloc(length + 1);
for (size_t i = 0; i < length; i++) {
size_t index;
while (index = rand_to_max(length), relocated[index])
;
dest[i] = text[index];
relocated[index] = true;
}
dest[length] = '\0';
printf("%s\n", dest);
return EXIT_SUCCESS;
}