You need to store the "encoded" symbols somewhere if you want to refer to them later - you put them in "newChar", but you constantly overwrite that value, so at the end of your loop only the last character is stored. No, writing them to the screen doesn't count as "storing" them. ;)/>
Putting them in a table is the best way to go. This is generally always the case when dealing with an unpredictable amount of data that you want to keep stored for later reference.
Note also that the "dec()" function doesn't return anything in your code. Hence the "enc()" function can't return the result of the "dec()" function.
function enc(incomingText)
local encodedStuff = {} -- Declares a new table, which'll only be available inside this one function.
for i = 1, #incomingText do
encodedStuff[i] = string.sub(incomingText, i, i) -- Put the current character in the table at position 'i'.
-- table.insert(encodedStuff, string.sub(incomingText, i, i)) -- Another way to do the same thing... Sorta.
print(encodedStuff[i]) -- Output what we put in the table to screen.
end
print("") -- "print" automatically performs a line break at the end, unlike "write".
return encodedStuff -- Passes the contents of our table to the caller as a "result".
end
function dec(incomingBytes) -- "incomingBytes" will be the table we got from the "enc()" function.
local decodedStuff = "" -- decodedStuff is local, so you can only access it in this function.
for i = 1, #incomingBytes do
decodedStuff = decodedStuff..string.char(incomingBytes[i])
print(string.char(incomingBytes[i]))
end
print("")
return decodedStuff -- This returns the content of the variable as a result.
end
local userInput = io.read() -- Do you mean just plain "read()"?
local encoded = enc(userInput) -- Pass the input to "enc()", put the results in "encoded"
local decoded = dec(encoded) -- Pass the encoded stuff to "dec()", put the result in "decoded"
print(decoded)
I haven't actually checked that the string manipulation functions do whatever is you want them to do, but this should give you a general understanding as to how you might go about doing things.