PDA

View Full Version : Excluding capital Is and lower Ls from a string


Amit1998
10/07/2017, 05:31 PM
Hi,
I'm using Ryder's random string generator and there's a slight confusion when it adds up a capital I or a lower L so I tried adjusting it in order to exclude these from the string but it didn't work:
stock randomString(strDest[], strLen = 11)
{
new letter[1];
while(strLen--)
{
letter[0] = random(2) ? (random(26) + (random(2) ? 'a' : 'A')) : (random(10) + '0');
while(!strcmp(letter[0], "I", false) || !strcmp(letter[0], "l", false))
{
letter[0] = random(2) ? (random(26) + (random(2) ? 'a' : 'A')) : (random(10) + '0');
}
strDest[strLen] = letter[0];
}
}

What could be the problem? Thanks.

Kaperstone
10/07/2017, 06:01 PM
*deleted*

OneDay
10/07/2017, 06:05 PM
stock randomString(strDest[], strLen = 11)
{
new letter;
while(strLen--)
{
letter = random(2) ? (random(25) + (random(2) ? 'a' : 'A')) : (random(10) + '0');
if (letter == 'I')
letter = 'Z';
else if (letter == 'l')
letter = 'z';
strDest[strLen] = letter;
}
}


Generates less letters and swaps some.

NaS
10/07/2017, 06:10 PM
Why do you use strcmp for comparing one letter?

Just do

if(letter[0] == 'I' || letter[0] == 'l')

You could also use a do while loop instead of using the same instruction twice:


do
{
letter[0] = random(2) ? (random(26) + (random(2) ? 'a' : 'A')) : (random(10) + '0');
} while(letter[0] == 'I' || letter[0] == 'l');


Other than that it should work.

EDIT: Oops too late.