Add random string generator function

This commit is contained in:
lakinduakash 2019-04-16 09:42:40 +05:30
parent e0777ff70c
commit b19924c6c9
2 changed files with 16 additions and 0 deletions

View File

@ -3,6 +3,8 @@
//
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "util.h"
int find_str(char *find, const char **array, int length) {
@ -18,3 +20,16 @@ int find_str(char *find, const char **array, int length) {
return -1;
}
void rand_str(char *dest, size_t length) {
char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (length-- > 0) {
size_t index = (size_t) ((double) rand() / RAND_MAX * (sizeof charset - 1));
*dest++ = charset[index];
}
*dest = '\0';
}

View File

@ -6,5 +6,6 @@
#define WIHOTSPOT_UTIL_H
int find_str(char *find, const char **array, int length);
void rand_str(char *dest, size_t length);
#endif //WIHOTSPOT_UTIL_H