Solved

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

Posted on 2000-03-07
5
249 Views
Last Modified: 2010-04-15
Thanks!
0
Comment
Question by:huhulucy90
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:ibo
ID: 2593458
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
 
LVL 3

Accepted Solution

by:
ibo earned 50 total points
ID: 2593491
oh oh.. rand()

make it somethin like

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

correction : 97 not 95 :/
0
 
LVL 84

Expert Comment

by:ozo
ID: 2593650
you can also use 26 instead of 25
0
 

Author Comment

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

0
 

Author Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 131
An API detour question 7 93
outlook office 365 8 144
How to pass pointer to int when expected type is pointer to const char *   ? 6 36
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question