I am trying to make something that will replace each character that is not a space in a string with a random letter from my list of letters. How would I go about doing that?
Example: “Hello, how are you?” → “UEJHE JEU ICO PEP”
Can i do something like:
for i = 1, stringlength, 1
if its not a space then
sets it to math.random[letters]
local t = {'a' , 'b' , 'c' , 'd'}
local s : String = -- some random string
for i = 1 , #s do
if(s[i] == '') then -- checking if this is a whitespace
return
end
local randomLetter = t[math.random(1,#t)]
s[i] = randomLetter
end
In Lua, either character can be used to open a string value providing the same character is used to close the string value.
Here’s a more comprehensive implementation of what you were looking for.
local randomObject = Random.new(tick())
local oldString = "Hello, how are you?"
local newString = ""
for char in oldString:gmatch(".") do
if char:match("%s") then
newString..=char
else
local int = randomObject:NextInteger(97, 122)
if int % 2 == 0 then
newString..=string.char(int):upper()
else
newString..=string.char(int):lower()
end
end
end
print(newString)
Here’s a sample of ten randomly generated results: