Reversing strings in an array with Swift

I want to change an array of strings. For example, the original array will look like this:

var array = ["lizard", "Rhino", "Monkey"]

      

and the end result should be those words that were canceled in the same order as in the same array:

["drazil", "onihR", "yeknoM"]

      

what i have now and i am modifying the strings correctly, but it creates 3 separate arrays and all strings are comma separated.

var array = ["lizard", "Rhino", "Monkey"]

for index in (array) {
  println(reverse(index))
}

[d, r, a, z, i, l]
[o, n, i, h, R]
[y, e, k, n, o, M]

      

any help would be appreciated thanks to you.

+3


source to share


2 answers


A shorter way is to display an array of strings and strikethrough characters and then initialize the string from them.

Swift 2.0:

let array = ["lizard", "Rhino", "Monkey"]
let reversed = array.map() { String($0.characters.reverse()) }
print(reversed) // [drazil, onihR, yeknoM]

      



Swift 1.2:

let array = ["lizard", "Rhino", "Monkey"]
let reversed = array.map() { String(reverse($0)) }
print(reversed) // [drazil, onihR, yeknoM]

      

+7


source


For fast 3.0

func reverseAString(value :String) -> String {
    //value
    return  (String(value.characters.reversed()))
}

      



Here it is!

+3


source







All Articles