logo

How to generate random strings in C

#include <stdio.h> #include <stdlib.h> #include <time.h> void rand_string(char* str, int num) { for (int i = 0; i < num; i++) { str[i] = rand() % ('z' - 'a' + 1) + 'a'; } str[num] = 0; } int main(int argc, char* argv[]) { srand(time(NULL)); char str[16]; rand_string(str, 15); printf("%s\n\n", str); return 0; }