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.

40,023 questions

51,974 answers

573 users

How to initialize char array with random characters in C++

1 Answer

0 votes
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
 
const int SIZE = 10;
 
void initialize_char_array_with_random_characters(char arr[], int size) {
    const std::string characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
     
    std::srand(std::time(nullptr));
         
    for (size_t i = 0; i < size; i++) {
        arr[i] = characters[std::rand() % characters.length()];
    }
}
 
int main() {
    char arr[SIZE] = "";
    int size = sizeof(arr) / sizeof(arr[0]);
  
    initialize_char_array_with_random_characters(arr, size);
  
    for (char ch : arr) {
        std::cout << ch << " ";
    }
}
 
   
   
/*
run:
   
n O I P c w 7 9 H N 
   
*/

 



answered May 18, 2024 by avibootz
edited May 18, 2024 by avibootz

Related questions

1 answer 106 views
1 answer 63 views
1 answer 77 views
1 answer 85 views
1 answer 92 views
1 answer 79 views
...