Write a Swift program to move the last two characters of a given string to the start string


Introduction

I have used Swift for windows 1.9 for debugging purpose. But you can use any Swift programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. func swapletters(_ str1: String) -> String {
  2. var chars = str1.characters
  3. let last_char = chars.removeLast()
  4. let rest_part = chars.removeLast()
  5. chars.insert(last_char, at: chars.startIndex)
  6. chars.insert(rest_part, at: chars.startIndex)
  7.  
  8. return String(chars)
  9. }
  10. print(swapletters("tech"))
  11. print(swapletters("study"))

Result

swift programming languages techstudy.org

     





Related Topics





Popular Posts