Re: Sorry, newbie question about generating a random string



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.
.



Relevant Pages

  • Re: Is this string input function safe?
    ... return a pointer to mallocated memory holding one input string, ... See my comment after your call to fgets. ... char* malloc_getstr ... before any characters are read, then the ...
    (comp.lang.c)
  • Re: is there an alternative to strstr
    ... >> To exploit this fact, you need a different data format, a plain string is ... > Ok I have put the email ids in a sorted array. ... However you can use an array of char pointers and use ... int cmp(const void *v1, const void *v2); ...
    (comp.lang.c)
  • Re: Pointers on string members of structure
    ... because this just points memstr to a fixed string and it is undefined to ... char memstrA; ... string array directly like this and how? ... struct or if the struct member points to the array. ...
    (microsoft.public.vc.language)
  • Re: RfD: XCHAR wordset (for UTF-8 and alike)
    ... extended to work with xchars, ... >replacing one char with another. ... before-cursor part, even for fixed-width characters. ... So, should string words ...
    (comp.lang.forth)
  • RE: Fixed Length
    ... match these formats, but rather pull the existing, normal, formats from the ... and run a function to build that information into a string that you ... length and the total lenght of all the fields has to be 100 characters. ... JobId (8 char) ...
    (microsoft.public.access.modulesdaovba)

Loading