Hey, im trying to create a string like the following in c++ but am unable to find a example in the internet in order to help me as im new to c++ and still trying to learn.

XXXXX-XXXXX-XXXXX-XXXXX

  • Custom Length
  • Custom Block Size

if someone would mind how would i create a random 25 character string and tokenize it like the example above, thanks for your help, it means alot to me and look forward to your answers.

I know how this is done with lua but im looking to make this into another DLL for a project im working on, thanks :)

Nathaniel Blackburn

You need to be much more specific with your request. Are you trying to make a 25 char string using 5 separate, unrelated 5 char substrings? Can the substrings be of variable length as long as the total char in the string is 25? What characters are valid to use? If alphabetical characters are used is it going to be case specific? What do you mean by random string?

You need to be much more specific with your request. Are you trying to make a 25 char string using 5 separate, unrelated 5 char substrings? Can the substrings be of variable length as long as the total char in the string is 25? What characters are valid to use? If alphabetical characters are used is it going to be case specific? What do you mean by random string?

Sorry, im trying to make a random string that's tokenized, it can have any length or block length that would be specified with all parameter, upper-case, lower-case, numeric cases are allowed but no special characters as i don't think there needed. Hope this clears my request up a little more basically its like a license code style (i.e 9EDAF0-134556-7477C5-E0BC8F), thanks again.

I'd write a function that returns a random chracter. The function would have a loop that calls rand() to get a random number between 48 (which is the letter '0') and 122 (the letter 'z'). Now there are a few special characters between those two numbers, so you will want to check whether is is alpha-numeric by using the macro isalnum() found in ctype.h. If it fails the test then loop back and get another random number.

The calling function would do whatever it needs to do with that random character to format the string.

Some hints :

const char minLetter = 'A';
const char maxLetter = 'Z';
char randomChar = char( minLetter + rand() % (maxLetter - minLetter + 1));//returns a random character from [minLetter,maxLetter]
const int minInt = 0;
const int maxInt = 9;
int randomDigit = minInt + rand() % (maxInt - minInt + 1); // returns a random number from [minInt,maxInt]

Thanks for your support guys :)

It sounds like you are trying to generate a hash function - take arbitrary string (of any length), and convert it into a numerical (hexadecimal) value. Is that the case? If so, there are a number of hash functions that you can use as examples, such as md5, sha1, etc.

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.