Write a Swift program to add the last character (given string) at the front and back of a given string. The length of the given string must be 1 or more.

bookmark

func front_back(str1: String) -> String {
    var result_word = str1
    let last_char = result_word.characters.removeLast()
    let last_str = String(last_char)
    return last_str + str1 + last_str
}
print(front_back(str1: "swift")) 
print(front_back(str1: "html")) 
print(front_back(str1: "h")) 

 

Output:

tswiftt
lhtmll
hhh