I would like to know how I’d do a system which replaces characters in a word, for example “watermelon” with random symbols such as !@#$%^&*, so the end result would be something like “w&*erme@!$n”, but it should also have some sort of configurable multiplier, like if it was set to 100 then all the characters in the message would be replaced with symbols, and if set to lower multiplier then the lesser the characters that would be scrambled/replaced.
This requires a bit of knowledge of in string manipulation, and should be looked into more indepth on your side to really understand the code being provided.
and
Each character in on your computer including the characters you want have numbers associated with them, this is their ‘byte’.
Why is this important? Well, because we can change the bytes of specific letters in the word to change them into the symbols you want.
First we need to split the word up into individual characters, we can achieve this by using string.split()
function StringToSpecialChar(Word)
local NewString = ""
local SplitString = string.split(Word, "")
return NewString
end
print(StringToSpecialChar("Watermelon"))
“SplitString” will return a table of all the characters of ‘Watermelon’. Our next step is to change these characters and find out how we are gonna do a percentage
function StringToSpecialChar(Word, Percentage)
local NewString = "" -- The Output (Placeholder)
local SplitString = string.split(Word,"") -- Table of all the characters of the word
local tableOfSpecialChar = {33,64,35,36,94,38,42} -- Table of all the special Characters
for i, v in pairs(SplitString) do -- Loops through all the characters in the word. Meaning "v" == The character
local NewChar -- A variable for setting a the new character
if math.random(100) <= Percentage then -- Checks if the percentage is greater than the math.random(100) output
NewChar = string.char(tableOfSpecialChar[math.random(#tableOfSpecialChar)]) -- Sets the newChar to a random special Character
NewString = NewString.. NewChar -- Makes the new string equal itself and then add the new Char. For example, "Wa" is what we have as newString then we add the newChar. "Wa" + "!" = "Wa!"
else
NewChar = v -- Sets the newChar to the character it would be so for example, if we are in the loop and we are the "t" in Watermelon, It'll just set the newChar to 't' and added it to the newString, so it doesn't change anything
NewString = NewString.. NewChar
end
end
return NewString -- Returns the newlyConstructed string
end
print(StringToSpecialChar("Watermelon", 25)) -- Performs function
-- StringToSpecialChar("Watermelon", 25)
Guess, I should explain. First we got the byte numbers of the special character and put them in a table, we than looped through the table of “SplitString” to be able to change each individual character. I added a new parameter on the function called ‘Percentage’, the number of percentage will be compared to a math.random of 100, in which if the math.random is lower than the percentage it’ll change the char, this correctly represents a percentage because the probability of the math.random(100) being lower than the percentage you set is proportionate. For example,
Percentage = 75
math.random(100) <= Percentage
There is a 75% chance math.random(100) is gonna be less than 75 because there is 25 numbers it can land on to be greater which would be the 25% chance it doesnt change a letter, and there are 75 numbers it could land on to be less than the percentage.
Once we get that we just need to set the newString to itself and add the character via combining each char with the “…”
This is a very watered down explanation, check the links above all this to get a full grasp of what I did.
Hey
, I noticed that in @StraightScared script that the percentage is not accurate (it is possible for 20% to change no letters). So I created my own code following the same Idea as his.
local function convertString(str,percentage)
local letters = string.split(str,"") -- Turning string into a table
local affected = {}
local finalString = ""
local characters = {"#","@","!","$","&","*","%"} -- Characters that can be used to replace
for i = 1,math.round(percentage/#str) do -- divides percentage to get the affected number of letters (eg. 20/10 = 2 letters affected)
local index
repeat index = math.random(#letters) until not table.find(affected,index) -- Making sure we don't pick the same letter twice
table.insert(affected,index)
end
for i,v in pairs(letters)do
if table.find(affected,i)then
finalString = finalString .. characters[math.random(#characters)] -- Selecting random character
else
finalString = finalString .. v
end
end
return finalString
end
print(convertString("Watermelon",20)) -- Wa%erm*lon
I was also bored so 
You are right, since it is a percentage chance, there is a chance that it never changes any of the letters. Although I do want to point at the script above also wouldn’t be accurate, for example, if the percentage was 50 and the word lets said had 20, the result would be 3. And 50% of 20 is not 3.
To get an accurate number you would need to do this
math.floor((Percentage/100) * #str)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.