Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,933 questions

51,870 answers

573 users

How to initialize char array with random characters in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10

void initialize_char_array_with_random_characters(char arr[], int size) {
    const char characters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int characters_count = sizeof characters;
    
    srand(time(NULL));
       
    for (size_t i = 0; i < size; i++) {
        arr[i] = characters[rand() % characters_count];
    }
}

int main() {
    char arr[SIZE + 1] = "";

    initialize_char_array_with_random_characters(arr, SIZE);

    for (int i = 0; i < SIZE; i++) {
        printf("%c ", arr[i]);
    }

    return 0;
}

 
 
 
/*
run:
 
a J y F 8 1 l E 4 I 
 
*/

 



answered May 17, 2024 by avibootz

Related questions

1 answer 102 views
1 answer 63 views
1 answer 76 views
1 answer 85 views
1 answer 90 views
1 answer 78 views
...