-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-string-prefix.c
More file actions
38 lines (28 loc) · 829 Bytes
/
random-string-prefix.c
File metadata and controls
38 lines (28 loc) · 829 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define Length 6
const char * sample(const char set[]) {
return &set[arc4random_uniform(strlen(set))];
}
int main(void) {
const char letters[] = "bcdfghjlmnpqrstvwz";
const char digits[] = "269";
const bool noLeadingDigit = true;
char all[strlen(letters) + strlen(digits)];
snprintf(&all[0], sizeof(letters), "%s", letters);
snprintf(&all[strlen(letters)], sizeof(digits), "%s", digits);
const size_t len = Length;
char out[len + 1];
size_t i = 0;
if (noLeadingDigit) {
snprintf(&out[0], sizeof(char) * 2, "%c", *sample(letters));
i++;
}
while (i < len ) {
snprintf(&out[i++], sizeof(char) * 2, "%c", *sample(all));
}
printf("%s\n", out);
return 0;
}