I'm new to NIm and I'm doing my first program, but I have a syntax problem that I can't discover and tried several alternatives but they don't work for me What's the problem with line 11?
#reverse cipher
var message: string = "mensaje secreto"
var translated: string = ""
var i: int
i = ((message.len) - 1)
echo (translated)
translated.add(message[i])
echo message[i]
And a modified faster variant:
#reverse cipher
var
message = "mensaje secreto"
i = message.high
translated: string = newString(i + 1)
for j, c in message:
translated[i - j] = c
echo translated
If you really are only reversing, that is an included battery:
import algorithm
var message = "mensaje secreto"
message.reverse
echo message
# Another way...
let translated = "mensaje secreto".reversed
echo cast[string](translated)
(The cast is only need to format like a string not a seq[char].)No great reason. I was probably too suggestible Re performance concerns which likely don't matter (yet!). I almost said "there are several ways to do this". That'll teach me to be brief! Lol. So, here, @deoxyt2...
import strutils; echo translated.join # A 3rd way
It actually might be a 5th way if you count the @miran and @Stefan_Salewski answers. And, yes, there are probably also several more, just within the stdlib. Might help newcomers to see various approaches. (I've often thought that RosettaCode could benefit from more attempts at "several answers" for each problem.)