Reverse words in a sentence - Swift

后端 未结 5 841
不知归路
不知归路 2021-01-29 15:43

How to perform reversing of all words in a sentence.

Example

let str = \"Hello playground\"

Result should be like \"olleH dnuorg

5条回答
  •  走了就别回头了
    2021-01-29 16:23

    Working code!

     import UIKit
    
     var str = "Hello playground"
    
     let result = str.split(separator: " ").map { String($0.reversed())}.joined(separator: " ")
     print(result) // olleH dnuorgyalp
    

提交回复
热议问题