#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static char *generate_random_string(char *str, size_t size);
int main()
{
char *s = NULL;
srand((unsigned)time(NULL));
s = generate_random_string(s, 10);
if (s) {
printf("%s\n", s);
free(s);
}
return 0;
}
static char *generate_random_string(char *str, size_t size) {
const char characters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int characters_count = sizeof characters;
if (size) {
str = malloc(size + 1);
if (!str) {
printf("malloc error\n");
return 0;
}
for (size_t i = 0; i < size; i++) {
str[i] = characters[rand() % characters_count];
}
str[size - 1] = '\0';
}
return str;
}
/*
run:
nEVJPpZ56F
*/