• C

How to use rand to generate a four-letter word?

Thanks!
huhulucy90Asked:
Who is Participating?
 
iboConnect With a Mentor Commented:
oh oh.. rand()

make it somethin like

word[i] = (int) (rand() % 25) + 97;

correction : 97 not 95 :/
0
 
iboCommented:
try this.. not very clever but i think this will work :)

char word[5]="";

....

randomize();
word[0] = random(25) + 1 + 95;
word[1] = random(25) + 1 + 95;
word[2] = random(25) + 1 + 95;
word[3] = random(25) + 1 + 95;
word[4] = '\x0';

printf("word = %s\n", word);

....


btw, inlcude stdlib and time libraries.
magic number 95? thats for lower case
65 for upper case (i think).
0
 
ozoCommented:
you can also use 26 instead of 25
0
 
huhulucy90Author Commented:
Thank you all for your help!
I've got the output like:
word = qmzr

0
 
huhulucy90Author Commented:
and I also add a sentence like:
srand(time(NULL));
so get a different output each time.
cheer!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.