Digamos que eu gosto de usar o while:
function reverse(str)
local t = string.explode(str, " ")
x = #t
y = ""
while x > 0 do
_ = x-1 > 0 and t[x].." " or t[x]
y = y.._
x = x-1
end
return y
end
print(reverse("Ola oi ss kk"))
info
Grupo: Infante
Registrado: 04/03/11
Posts: 1.6k
Reputação: +393
Char no Tibia: No Have

Digamos que eu gosto de usar o while:
function reverse(str)
local t = string.explode(str, " ")
x = #t
y = ""
while x > 0 do
_ = x-1 > 0 and t[x].." " or t[x]
y = y.._
x = x-1
end
return y
end
print(reverse("Ola oi ss kk"))
Editado Maio 9 por caotic
ficou até boa, mas você não precisa transformar em tabela pra depois transformar em string, pode fazer direto como string, assim:
function explode(e, sep)
local result = {} e:gsub("[^".. sep .."]+", function(s) table.insert(result, s:match("^%s*(.-)%s*$")) end)
return result
end
function reverse(str)
local ret_str = ""
local words = explode(str, " ")
for i = #words, 2, -1 do
ret_str = ret_str .. words[i] .. " "
end
ret_str = words[1] and ret_str .. words[1] or ""
return ret_str
end
Editado Maio 10 por dalvorsn
info
Grupo: Marquês
Registrado: 02/08/12
Posts: 1k
Reputação: +234
Char no Tibia: Maurolkit

ficou até boa, mas você não precisa transformar em tabela pra depois transformar em string, pode fazer direto como string, assim:
function explode(e, sep) local result = {} e:gsub("[^".. sep .."]+", function(s) table.insert(result, s:match("^%s*(.-)%s*$")) end) return result end function reverse(str) local ret_str = "" local words = explode(str, " ") for i = #words, 2, -1 do ret_str = ret_str .. words[i] .. " " end ret_str = words[1] and ret_str .. words[1] or "" return ret_str end
Hum, boa, mas fiz na hora só tinha pensado desse modo ;d.
info
Grupo: Marquês
Registrado: 02/08/12
Posts: 1k
Reputação: +234
Char no Tibia: Maurolkit
G712z042[/pastebin]
Input:
Output: