SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 29/12/2016, 02:36 PM   #1
GoldenLion
Gangsta
 
GoldenLion's Avatar
 
Join Date: Sep 2014
Location: Estonia
Posts: 945
Reputation: 142
Default change the position of letters

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?
GoldenLion is offline   Reply With Quote
Old 29/12/2016, 02:47 PM   #2
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,944
Reputation: 504
Default Re: change the position of letters

PHP Code:
   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); 
SyS is offline   Reply With Quote
Old 29/12/2016, 04:51 PM   #3
GoldenLion
Gangsta
 
GoldenLion's Avatar
 
Join Date: Sep 2014
Location: Estonia
Posts: 945
Reputation: 142
Default Re: change the position of letters

Thank you, it works, but sizeof(str) needs to be changed to strlen(str).
GoldenLion is offline   Reply With Quote
Old 30/12/2016, 11:37 AM   #4
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,944
Reputation: 504
Default Re: change the position of letters

Quote:
Originally Posted by GoldenLion View Post
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.
SyS is offline   Reply With Quote
Old 30/12/2016, 12:42 PM   #5
GoldenLion
Gangsta
 
GoldenLion's Avatar
 
Join Date: Sep 2014
Location: Estonia
Posts: 945
Reputation: 142
Default Re: change the position of letters

Quote:
Originally Posted by Sreyas View Post
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
GoldenLion is offline   Reply With Quote
Old 30/12/2016, 01:28 PM   #6
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,944
Reputation: 504
Default Re: change the position of letters

Quote:
Originally Posted by GoldenLion View Post
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.
SyS is offline   Reply With Quote
Old 30/12/2016, 05:29 PM   #7
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,768
Reputation: 754
Default Re: change the position of letters

pawn Code:
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;
}
SickAttack is offline   Reply With Quote
Old 30/12/2016, 05:39 PM   #8
admantis
High-roller
 
Join Date: Sep 2009
Posts: 2,524
Reputation: 449
Default Re: change the position of letters

Quote:
Originally Posted by SickAttack View Post
pawn Code:
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?
admantis is offline   Reply With Quote
Old 30/12/2016, 05:40 PM   #9
GoldenLion
Gangsta
 
GoldenLion's Avatar
 
Join Date: Sep 2014
Location: Estonia
Posts: 945
Reputation: 142
Default Re: change the position of letters

Quote:
Originally Posted by SickAttack View Post
pawn Code:
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;
}
Is that better than Sreyas' one?
GoldenLion is offline   Reply With Quote
Old 30/12/2016, 06:44 PM   #10
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,831
Reputation: 1401
Default Re: change the position of letters

Quote:
Originally Posted by admantis View Post
please explain how does it work?
https://en.wikipedia.org/wiki/Fisher...3Yates_shuffle

Quote:
Originally Posted by GoldenLion View Post
Is that better than Sreyas' one?
It is the modern and "inside-out" versions and to answer to your question: yes, it is.
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change start position raamiix Scripting Help 9 23/05/2013 02:02 PM
Need to change MAP position alex_sawa00 Scripting Help 2 30/01/2012 02:42 AM
Cars change position Dime Help Archive 15 13/08/2010 08:14 PM
Change Position By Timer for all Kitten Help Archive 4 09/08/2010 07:13 AM
Is there a way to change a lot of Z position to z-1000 ? [Ask]Terminator Help Archive 1 23/07/2010 08:51 PM


All times are GMT. The time now is 01:46 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.