How to generate 10 random 5 characters strings in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void rand_string(char *str, size_t size) {
    const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
    for (int i = 0; i < size; i++) {
        str[i] = charset[rand() % (int) (sizeof charset - 1)];
    }
    str[size] = '\0';
}
 
int main(void) {
    char s[6] = "";
    
    srand(time(0)); 
    
    for (int i = 0; i < 10; i++) {
        rand_string(s, 5);
        puts(s);
    }
     
    return 0;
}
 
 
 
 
/*
run:
   
oKbJx
eDZaE
hsKYp
OThOD
ayByp
QZXIg
ZZSAi
sflth
SaBEb
qSuzI
   
*/

 



answered Sep 29, 2021 by avibootz

Related questions

1 answer 174 views
1 answer 165 views
1 answer 150 views
1 answer 161 views
1 answer 168 views
1 answer 188 views
2 answers 181 views
...