How do I replace each character of a string that is not a space with a random letter?

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]

The code below should serve your purpose -

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
1 Like

Ok in previous languages I’ve been told not to use ’ ’ for strings is that not true for Roblox language?

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:

image

2 Likes

I do not understand what’s going on in the code there.