@MD_ItteshamItte/

Random word generator

C

No description

fork
loading
Files
  • main.c
main.c
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
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LENGTH  26                       // the cipher key length

void random_string(char * string, size_t length)
{
    char pool[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    size_t poolsize = strlen(pool);
    size_t index;
    size_t i;

    srand((unsigned)time(NULL));

    for(i = 0; i < length && poolsize > 0; ++i)
    {
        index = rand() % poolsize;       // a random index into the pool
        string[i] = pool[index];         // take that character
        pool[index] = pool[--poolsize];  // replace it with the last pool ...
    }                                    // ... element and shorten the pool

    string[i] = '\0';
}

int main(void)
{
    char s[LENGTH + 1];                  // adequate length
    random_string(s, LENGTH);
    printf("%s\n", s);
    return 0;
}