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.

 

func swapletters(_ str1: String) -> String {
    var chars = str1.characters
    let last_char = chars.removeLast()
    let rest_part = chars.removeLast()
    chars.insert(last_char, at: chars.startIndex)
    chars.insert(rest_part, at: chars.startIndex)
 
    return String(chars)
}
print(swapletters("tech"))
print(swapletters("study"))

Result

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

Leave a Comment