How do I replace random letters in a string?

Hello. I’m trying to make a radio system and I want to have it so the further the message sender is, the more distorted their incoming message is. I can take care of everything except for the most important part, which is replacing random letters in the string.

For example, let’s just use a period for simplicities sake.
Original message would be : “Hey! Get away from the blast zone!”
Distorted message would be : “He.! G.t aw.y fr.m t… bl.st …ne!”

It would also be cool if the letters they get replaced with could be stored in a table so I have control over what symbols can replace letters. i.e. Symbols={"$","%","^","&","(","@","#"}

Any guidance is appreciated.

something like this?

local str = "Hey! Get away from the blast zone!"
local repamt = 5
local repwith = {"$","%","^","&","(","@","#"}

repamt = repamt <= string.len(str) and repamt or string.len(str)
local newstr = string.split(str, "")

while repamt > 0 do
    repamt -= 1
    local repspot
    repeat
        repspot = string.len(str) > 1 and math.random(string.len(str)) or 1
    until not table.find(repwith, newstr[repspot]) and newstr[repspot] ~= " "
    
    newstr[repspot] = repwith[math.random(#repwith)]
end

local wholestr = ""

for i,v in pairs(newstr) do wholestr ..= v end

print(wholestr)

As an explanation, I continued to decrease the replace amount until it was 0, and for each replacement spot I would repeat getting a new number until the replace spot was not already done, then I would replace that spot in the table with a symbol/repwith.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.