I have small question:
How can I create a random string from 'a' to 'z'?
thank you!

Recommended Answers

All 3 Replies

Create a string containing all the letters you want to use. Seed and use rand() to access elements (using the % operator to force the numbers within the string's range). Strcat in a loop. Pseudo-code.

my_letters = "abcdefghijklmnopqrstuvwxyz" /* allowable letters for your random string */
my_random_string /* your random string. Initially empty */

srand( time(0) ) /* seed rand */

for i=0 to 10 /* or however long you want it */
  append( my_random_string,  /* append to this string */
          my_letters[rand()%strlen(my_random_string)] ) /* this letter */
end loop

Or something like that. Alternatively you could just rand() (limit up to 25) and add 'a' onto the number and append that to your string. I think the above method is better though because you can extend the characters you use (you could put numbers, symbols, capital letters in it). That is harder to do with the second method I mentioned.

Use a loop to generate as many random numbers from 0 to 25 as you want and add the values to the string. When you get the random number, simply add 'a' to it and you will have all letters instead of 0-25

Thank you for your helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.