SA-MP Forums Archive
change the position of letters - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: change the position of letters (/showthread.php?tid=625194)



change the position of letters - GoldenLion - 29.12.2016

Hi, is it possible to randomize the positions of the letters of a word? I mean like let's say you get a word "apple" then it changes the positions of the letters and one time it will look something like "plepa" and other time "eaplp" etc. I want it to be totally random so players could unscramble these words. I don't want to make it like that "apple" is always "eaplp" It needs to be different every time. Is that possible?


Re: change the position of letters - SyS - 29.12.2016

PHP код:
   new swap,index,i;
   new 
str[]="Hello";
   while(
str[i]!='\0'
    { 
        
swap=str[i]; 
        
index=random(sizeof(str)-1); 
        
str[i] = str[index]; 
        
str[index]=swap
        
i++; 
    }
    
printf(str); 



Re: change the position of letters - GoldenLion - 29.12.2016

Thank you, it works, but sizeof(str) needs to be changed to strlen(str).


Re: change the position of letters - SyS - 30.12.2016

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Thank you, it works, but sizeof(str) needs to be changed to strlen(str).
Not in this case as I assigned the string leaving the compiler to determining the cell required so we can avoid an overhead of strlen replacing with a constant which is better optmised.


Re: change the position of letters - GoldenLion - 30.12.2016

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
Not in this case as I assigned the string leaving the compiler to determining the cell required so we can avoid an overhead of strlen replacing with a constant which is better optmised.
Ah, alright. Well I did that in a command and used the params with that so it gave me an error. :P


Re: change the position of letters - SyS - 30.12.2016

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Ah, alright. Well I did that in a command and used the params with that so it gave me an error. :P
yeahhh my bad i forgot that it will give error in commonly using compiler (not in modern) as the array size is unknown due as checking is done before calculation.


Re: change the position of letters - SickAttack - 30.12.2016

pawn Код:
ScrambleString(string[])
{
    for(new i = 0, j = strlen(string), k = random(j); i < j; i ++, k = random(j))
    {
        i != k && (string[i] ^= string[k], string[k] ^= string[i], string[i] ^= string[k]);
    }
    return string;
}



Re: change the position of letters - admantis - 30.12.2016

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
pawn Код:
ScrambleString(string[])
{
    for(new i = 0, j = strlen(string), k = random(j); i < j; i ++, k = random(j))
    {
        i != k && (string[i] ^= string[k], string[k] ^= string[i], string[i] ^= string[k]);
    }
    return string;
}
please explain how does it work?


Re: change the position of letters - Konstantinos - 30.12.2016

Quote:
Originally Posted by admantis
Посмотреть сообщение
please explain how does it work?
https://en.wikipedia.org/wiki/Fisher...3Yates_shuffle

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Is that better than Sreyas' one?
It is the modern and "inside-out" versions and to answer to your question: yes, it is.


Re: change the position of letters - GoldenLion - 30.12.2016

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
https://en.wikipedia.org/wiki/Fisher...3Yates_shuffle



It is the modern and "inside-out" versions and to answer to your question: yes, it is.
Thanks.