Re: Sorry, newbie question about generating a random string
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Mon, 17 Mar 2008 15:19:52 -0500 (CDT)
On Sat, 8 Mar 2008 12:21:06 -0600 (CST), Scooter <slbentley@xxxxxxx>
wrote in comp.lang.c.moderated:
I'm trying to write a simple program to generate a random string. I
have what seems to work, although I'm guessing there are better ways
to do it. But my overall goal is to generate a random character, then
add a second random character to it, then another, etc. until the
string grows to a max of 10 characters. I'm testing a word wheel on a
website but have to use 'C' and it is not my forte'. Anyway, here's
what I have:
The real problem is that you are not terminating the string. In C, a
string is an array of characters ending in a null character, '\0'. If
an array of chars does include a terminating '\0', it is not a string
and should not be passed to any C function that requires a string,
including printf("%s").
There are two relatively easy ways to fix your problem:
int myRand,myLoop;
char myChar[1];
char myLoc[11];
Replace the line above with:
char myLoc[11] = "";
....or:
char myLoc[11] = { '\0' };
Either of these will ensure that all 11 characters in the array are
set to '\0', so as you add characters the string will always be
properly terminated.
Note that this works if you only use the string once after defining it
this way. So the other alternative, that works even if the array you
are starting with is not filled with null terminators, is to always
add one on the end yourself:
srand((unsigned int)time(0)); //Seed number for rand()
for (myLoop = 0; myLoop < 10; myLoop++) {
myRand = rand() % 25 + 65;
myChar[0] = (char)myRand;
myLoc[myLoop] = myChar[0] ;
myLoc[myLoop + 1] = '\0';
printf("%s", myLoc);
}
A few additional comments:
There is no need for myChar at all. Even if you did need it, there is
no reason to make it an array of one char instead of just a plain
char.
Finally, the cast to char is completely unnecessary and useless.
Conversion from int to char on assignment is automatic. Adding the
cast does not change the result in any way.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- References:
- Sorry, newbie question about generating a random string
- From: Scooter
- Sorry, newbie question about generating a random string
- Prev by Date: Slow moderation cycle
- Next by Date: Re: ANSI-C Data Type "Prototyping" & Circular-Referenced Definitions ...
- Previous by thread: Re: Sorry, newbie question about generating a random string
- Next by thread: Re: Sorry, newbie question about generating a random string
- Index(es):
Relevant Pages
|
Loading