Guest User

Text Encoder

a guest
Apr 23rd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. -- all your variables just in a table for ease
  2. local lookup = {
  3.   a = 'D', b = 'E', c = 'F', d = 'G', e = 'H', f = 'I', g = 'J', h = 'K', i = 'L', j = 'M', k = 'N', l = 'O', m = 'P', n = 'Q', o = 'R', p = 'S', q = 'T', r = 'U', s = 'V', t = 'W', u = 'X', v = 'Y', w = 'Z', x = 'A', y = 'B', z = 'C', a = 'D', b = 'E', c = 'F', d = 'G', e = 'H', f = 'I', g = 'J', h = 'K', i = 'L', j = 'M', k = 'N', l = 'O', m = 'P', n = 'Q', o = 'R', p = 'S', q = 'T', r = 'U', s = 'V', t = 'W', u = 'X', v = 'Y', w = 'Z', x = 'A', y = 'B', z = 'C', A = 'd', B = 'e', C = 'f', D = 'g', E = 'h', F = 'i', G = 'j', H = 'k', I = 'l', J = 'm', K = 'n', L = 'o', M = 'p', N = 'q', O = 'r', P = 's', Q = 't', R = 'u', S = 'v', T = 'w', U = 'x', V = 'y', W = 'z', X = 'a', Y = 'b', Z = 'c'
  4. }
  5.  
  6. -- encode is more accurate than encrypt
  7. function encode(str)
  8.   -- create our return string
  9.   local temp = ""
  10.   -- loop through the input string
  11.   for i = 1, #str do
  12.     -- get the current character
  13.     local char = str:sub(i,i)
  14.     -- get the current char's mapped value in the table, if it's not in the table, i.e. a space, then just use itself
  15.     local newChar = lookup[char] or char
  16.     -- append the newChar to the return value
  17.     temp = temp..newChar
  18.   end
  19.   -- return the new encoded string
  20.   return temp
  21. end
  22.  
  23. -- just some test code
  24. write('Text to be encoded: ')
  25. local input = read()
  26. local encoded = encode(input)
  27. print('Was: '..input..'\nNow: '..encoded)
Advertisement
Add Comment
Please, Sign In to add comment