Link to home
Start Free TrialLog in
Avatar of huhulucy90
huhulucy90

asked on

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

Thanks!
Avatar of ibo
ibo

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).
ASKER CERTIFIED SOLUTION
Avatar of ibo
ibo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
you can also use 26 instead of 25
Avatar of huhulucy90

ASKER

Thank you all for your help!
I've got the output like:
word = qmzr

and I also add a sentence like:
srand(time(NULL));
so get a different output each time.
cheer!