Good day! I'm a newbie programmer and I am still confused with how to apply the concepts of C.
I am working on a project. My problem is that I have INITIALIZED certain characters and have stored them in a string (vars[28]). My goal is to generate the characters of the string in a random manner and to store the generated string in another variable, which I do not know how to.
int randnum = 0, num = 0;
char vars[28] = "|abcdefghijklmonpqrstuvwxyz."; //initialized string
char term; //where to store randomized string
int i = 0;
char choi[1];
printf ("%c", vars[0]);
srand (time(NULL));
randnum = rand() % 30; //only 30 characters maximum to be stored
for (i = 0; i <= randnum; i++)
{
//randomly produce vars[28] characters here and store into 'term'
}
Additional question: How do I prevent | and . to be beside each other when randomized?
Thank you!
vars[rand() % 28]in a loop matching your generated string lengthtermis not an array nor achar*pointer to a stirng, it is a single byte,term[100]is accessing random memory location and can result segmentation fault.